2

I am fairly new to XML and parsing it with C#. I am trying to parse an XML document that looks like this:

<Msg Version="1.0">
  <ProgList>
    <update sequence="41248" amount="3327" Lvl="1" Grp="1" />
    <update sequence="41216" amount="5326" Lvl="2" Grp="1" />
    <update sequence="41252" amount="7326" Lvl="3" Grp="1" />
  </ProgList>
</Msg>

I'm trying to use XDocument and can't seem to parse out the three updates with the attributes.

However, if I create an XML document containing the same data in this form:

<Msg Version="1.0">
    <ProgList>
        <Level>
            <seq>41248</seq>
            <amount>3327</amount>
            <Lvl>1</Lvl>
            <Grp>1</Grp>
        </Level>
        <Level>
            <seq>41216</seq>
            <amount>5326</amount>
            <Lvl>2</Lvl>
            <Grp>1</Grp>
        </Level>
        <Level>
            <seq>41252</seq>
            <amount>7326</amount>
            <Lvl>3</Lvl>
            <Grp>1</Grp>
        </Level>
    </ProgList>
</Msg>

I can parse and iterate through the Level data using the following code:

String xmlText = String.Empty;
//String xml = String.Empty;
int begin = Data.IndexOf("<?xml");
int end = Data.IndexOf("</Msg") + 6;

try
{
    //xml = Data.Substring(begin, end - begin);
}
catch { };

String xml = 
@"<Msg Version='1.0'>
                                <ProgList>
                                    <Level>
                                        <seq>41248</seq>
                                        <amount>3327</amount>
                                        <Lvl>1</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                    <Level>
                                        <seq>41216</seq>
                                        <amount>5326</amount>
                                        <Lvl>2</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                    <Level>
                                        <seq>41252</seq>
                                        <amount>7326</amount>
                                        <Lvl>3</Lvl>
                                        <Grp>1</Grp>
                                    </Level>
                                </ProgList>
</Msg>";



try
{
    XDocument xmlDoc = XDocument.Parse(xml);

    var updates = from x in xmlDoc.Descendants("Level")
                    select new
                    {
                        seq = x.Descendants("seq").First().Value,
                        amt = x.Descendants( "amount" ).First().Value,
                        lvl = x.Descendants("Lvl").First().Value,
                        grp = x.Descendants("Grp").First().Value
                    };

    foreach (var x in updates)
    {
        String output = String.Format("{0}SEQ: {1} AMT: {2} LVL: {3} GRP: {4}",
                                Environment.NewLine, x.seq, x.amt, x.lvl, x.grp);
    }

How would I parse out the first XML document style?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Chimera
  • 5,884
  • 7
  • 49
  • 81

1 Answers1

6

Use the Attribute Method instead of the Descendants Method:

var updates = from x in xmlDoc.Root.Element("ProgList").Elements("update")
              select new
              {
                  seq = (int)x.Attribute("sequence"),
                  amt = (int)x.Attribute("amount"),
                  lvl = (int)x.Attribute("Lvl"),
                  grp = (int)x.Attribute("Grp"),
              };
dtb
  • 213,145
  • 36
  • 401
  • 431
  • 1
    Upvoted. I'd only add to this by asking the OP to look for other examples, http://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth, and look at the XDocument documentation at MSDN (http://msdn.microsoft.com/en-us/library/bb360635.aspx) - just to get used to how to navigate around an XML document. – dash Dec 01 '11 at 23:17
  • Thank you dtb and dash. I appreciate the help. – Chimera Dec 01 '11 at 23:31