Marc
Marc

Reputation: 13

Parsing XML string with C#

i'm newbie with c# and i want to parse this file:

<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><array><data>
<value><array><data>
<value><string>1</string></value>
<value><string>Tutos</string></value>
</data></array></value>
<value><array><data>
<value><string>3</string></value>
<value><string>BEBIDAS ALCOHOL</string></value>
</data></array></value>
<value><array><data>
<value><string>6</string></value>
<value><string>conse</string></value>
</data></array></value>
<value><array><data>
<value><string>7</string></value>
<value><string>Custom Packs</string></value>
</data></array></value>
<value><array><data>
<value><string>5</string></value>
<value><string> PRODUCT</string></value>
</data></array></value>
<value><array><data>
<value><string>4</string></value>
<value><string>PRODUCT BAAT</string></value>
</data></array></value>
<value><array><data>
<value><string>2</string></value>
<value><string>GGGGGr</string></value>
</data></array></value>
</data></array></value>
</param>
</params>
</methodResponse>

I have tried with XMLnode but with this code, the count of xnList is 0.

XmlDocument xml = new XmlDocument();
xml.LoadXml(mensaje);
XmlNodeList xnList=xml.SelectNodes("/methodResponse/params/param/array/data");

If i write:

XmlNodeList xnList=xml.SelectNodes("/methodResponse/params/param");

The parameter nxList.Count is 1. I think the problem is c# doesn't recognize the labels array or data. Can someone how solve it?

Upvotes: 1

Views: 187

Answers (1)

Aldracor
Aldracor

Reputation: 2181

It seems you XML path is incorrect. Try:

XmlNodeList xnList = xml.SelectNodes("/methodResponse/params/param/value/array/data");

The first item in the xnList, should then be:

<value>
    <array>
        <data>
            <value>
                <string>1</string>
            </value>
            <value>
                <string>Tutos</string>
            </value>
        </data>
    </array>
</value>

Also you might want to look at making the XML less complex (easier to read/use), if you have access to do this (less sub nodes). As you can see to navigate through 11 nodes to get to the first real value can be quite confusing

I formatted the XML so you can see how deep it really is:

<?xml version='1.0' ?>
<methodresponse>
  <params>
    <param>
      <value>
        <array>
          <data>
            <value>
              <array>
                <data>
                  <value>
                    <string>1</string>
                  </value>
                  <value>
                    <string>Tutos</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>3</string>
                  </value>
                  <value>
                    <string>BEBIDAS ALCOHOL</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>6</string>
                  </value>
                  <value>
                    <string>conse</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>7</string>
                  </value>
                  <value>
                    <string>Custom Packs</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>5</string>
                  </value>
                  <value>
                    <string> PRODUCT</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>4</string>
                  </value>
                  <value>
                    <string>PRODUCT BAAT</string>
                  </value>
                </data>
              </array>
            </value>
            <value>
              <array>
                <data>
                  <value>
                    <string>2</string>
                  </value>
                  <value>
                    <string>GGGGGr</string>
                  </value>
                </data>
              </array>
            </value>
          </data>
        </array>
      </value>
    </param>
  </params>
</methodresponse>

Upvotes: 2

Related Questions