What's the easiest way to Parse an XML file with many different levels?

i know this is a long question.. but I'm going crazy here trying to learn one of the many ways to parse an XML document... Alot of the examples I have online are very simple XML documents.. but I have something multi leveled and I would like to get this parsed..

my class code here: http://pastebin.com/VKbWzKHG

<?xml version="1.0" encoding="utf-8"?>
<PageList>
    <Page>
        <UpperLeft>
            <Parameters>
                <League>AL</League>
                <Season>R</Season>
                <Category>Runs</Category>
                <RankOrder>n/a</RankOrder>
            </Parameters>
            <Info>
                <Row1>
                    <TeamLogo>LAA.tif</TeamLogo>
                    <Rank>1.</Rank>
                    <Name>Trout</Name>
                    <Stat>109</Stat>
                </Row1>
                <Row2>
                  .
                  .
                  .
                </Row5>
            </Info>
        </UpperLeft>
        <UpperRight>
            .
            .
            .
        </UpperRight>
        <LowerLeft>
            .
            .
        </LowerLeft>
        <LowerRight>
            .
            .
        </LowerRight>
    </Page>
</PageList>

pretty much there will be a lot of pages.. i didn't want to paste the full thing so i tried to cut it out.. but this is the structure:

page
  --> upper left
      --> parameters
          --> league
          --> season
          --> category
          --> rankorder
      --> info
          --> row 1
              --> team logo
              --> rank
              --> name
              --> stat
          --> row 2 to row 5
  --> upper right, lower left, lower right
end page

How can I successfully parse this nightmare? Everything is a class that I created that was nice and neat.. but trying to put everything back into a class i'm just having so much trouble..

XDocument xdoc;
        try
        {
            List<MLBPage> collection =
            {
                from e in XDocument.Load("PageList.xml").Root.Elements("PageList")

                select new MLBPage
                {
                    ul = new Quadrant  //upper left
                    {
                        qp = new QuadrantParameters
                        {
                            //league, season, category, rank order here
                        },
                        qi = new QuadrantInfo
                        {
                            //team logo, rank, name, stat here
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            /

Answers


You could start mapping the classes/attributes/elements:

[Serializable] //Root
[XmlRoot(ElementName = "InfXml", Namespace="the namespace URI")]
public class InfXml
{
    [XmlAttribute(AttributeName = "id")] //attribute
    public string Id { get; set; }
    public bool ShouldSerializeId() //Should serialize, only serializes if not null.
    {
        return !string.IsNullOrEmpty(Id); //This is only for optional fields.
    }

    [XmlElement(ElementName = "Identification")] //Non optional group.
    public Identification Identification{ get; set; }

    [XmlElement(ElementName = "Adress")] //Optional group.
    public Adress Adress{ get; set; }
    public bool ShouldSerializeAdress()
    {
        return Adress!= null;
    }
}

If you have a mapped class-to-Xml, you can use this 2 methods:

const string PrefixXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

public static object Deserialize<T>(T obj, string xmlText)
{
    try
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(T));
        TextReader textReader = new StringReader(xmlText);
        return (T)deserializer.Deserialize(textReader);
    }
    catch (Exception ex)
    {
        //Catch here.
        return null;
    }

}

public static XmlDocument Serialize<T>(T obj)
{
    string xmlString = GerarXml.Gerar<T>(obj);
    if (!xmlString.Contains("xml version="))
    {
        xmlString = PrefixXml + xmlString;
    }
    xmlString = xmlString.Replace(Environment.NewLine, string.Empty);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlString);
    return doc;
}

And if you want to get the XML as a string:

public static string GetXmlText(XmlDocument doc)
{
    StringWriter stringWriter = new StringWriter();
    XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

    doc.WriteTo(xmlTextWriter);

    string ret = stringWriter.ToString();

    ret = ret.Replace(Environment.NewLine, string.Empty);
    if (!ret.Contains("xml version="))
    {
        return PrefixXml + ret;
    }
    return ret;

}

Need Your Help

Dart: String concatenation error: expected type is 'num'?

compiler-errors dart string-concatenation

In my Dart app I need to have a const string variable defined like so: