Link to home
Start Free TrialLog in
Avatar of Silas2
Silas2

asked on

XML Document Question

I've got the following XML:
<Widgets   xmlns="http://www.tryme.com">
  <Widget name="Pipe"  caption="Pipe">
    <Filters>
      <Filter member="Kind" operator="IsEqualTo" value="2" valueType="Int32" />
    </Filters>
    <Access>
      <Field name="Code" lock="true" />
      <State condition="Id = 0">
        <Field name="Code" lock="false" />
      </State>
      <State condition="IncreseMethodCode = 1">
        <Field name="Increse" lock="true" />
      </State>
      <State condition="IncreseMethodCode = 2">
        <Field name="IncresePeriodInMonths" lock="true" />
      </State>
    </Access>
  </Widget >
</Widgets>

Open in new window

I need to get a collection of the 'States' for the Widget with name "Pipe". I've got this code from the online sample, but I'm trying to get a neat way of getting the 'States' in one go.
   XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("bk", "http://www.tryme.com");
            string xPathString = "//bk:Widgets/bk:Widget[@name='Pipe']";

            XmlNode xmlNode = doc.DocumentElement.SelectSingleNode(xPathString, nsmgr);
            XmlNodeList list = xmlNode.ChildNodes;
            return list ;

Open in new window

Avatar of zc2
zc2
Flag of United States of America image

You could use the SelectNodes() method instead of SelectSingleNode() with the XPath which selects the States:
string xPathString = "//bk:Widgets/bk:Widget[@name='Pipe']/bk:State";
that will give you the list of State nodes, so you won't need to deal with the ChildNodes collection anymore.
Avatar of Silas2
Silas2

ASKER

Thanks for having a look for me. I tried that and:
//bk:Widgets/bk:Widget[@name='Pipe']/bk:States
(with 's' at the end)
and :
//bk:Widgets/bk:Widget[@name='Pipe']//bk:Access/bk:States
but neither seems to yield anything, is it because <States> is nested in Access?
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Silas2

ASKER

Thanks, that's got it.