Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed info on Parsing XML in .NET 4
    primarykey
    data
    text
    <p>I am fairly new to XML and parsing it with C#. I am trying to parse an XML document that looks like this:</p> <pre><code>&lt;Msg Version="1.0"&gt; &lt;ProgList&gt; &lt;update sequence="41248" amount="3327" Lvl="1" Grp="1" /&gt; &lt;update sequence="41216" amount="5326" Lvl="2" Grp="1" /&gt; &lt;update sequence="41252" amount="7326" Lvl="3" Grp="1" /&gt; &lt;/ProgList&gt; &lt;/Msg&gt; </code></pre> <p>I'm trying to use XDocument and can't seem to parse out the three updates with the attributes.</p> <p>However, if I create an XML document containing the same data in this form:</p> <pre><code>&lt;Msg Version="1.0"&gt; &lt;ProgList&gt; &lt;Level&gt; &lt;seq&gt;41248&lt;/seq&gt; &lt;amount&gt;3327&lt;/amount&gt; &lt;Lvl&gt;1&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;Level&gt; &lt;seq&gt;41216&lt;/seq&gt; &lt;amount&gt;5326&lt;/amount&gt; &lt;Lvl&gt;2&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;Level&gt; &lt;seq&gt;41252&lt;/seq&gt; &lt;amount&gt;7326&lt;/amount&gt; &lt;Lvl&gt;3&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;/ProgList&gt; &lt;/Msg&gt; </code></pre> <p>I can parse and iterate through the Level data using the following code:</p> <pre><code>String xmlText = String.Empty; //String xml = String.Empty; int begin = Data.IndexOf("&lt;?xml"); int end = Data.IndexOf("&lt;/Msg") + 6; try { //xml = Data.Substring(begin, end - begin); } catch { }; String xml = @"&lt;Msg Version='1.0'&gt; &lt;ProgList&gt; &lt;Level&gt; &lt;seq&gt;41248&lt;/seq&gt; &lt;amount&gt;3327&lt;/amount&gt; &lt;Lvl&gt;1&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;Level&gt; &lt;seq&gt;41216&lt;/seq&gt; &lt;amount&gt;5326&lt;/amount&gt; &lt;Lvl&gt;2&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;Level&gt; &lt;seq&gt;41252&lt;/seq&gt; &lt;amount&gt;7326&lt;/amount&gt; &lt;Lvl&gt;3&lt;/Lvl&gt; &lt;Grp&gt;1&lt;/Grp&gt; &lt;/Level&gt; &lt;/ProgList&gt; &lt;/Msg&gt;"; 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); } </code></pre> <p>How would I parse out the first XML document style?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload