1

I have included an XML file in my InfoPath form as a secondary data source. The data connection is named Divisions. Below is the structure and content of the file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Divisions>
    <Division>
        <Name>Business Application Services</Name>
        <Abbr>BAS</Abbr>
    </Division>
    <Division>
        <Name>Network Security Services</Name>
        <Abbr>NSS</Abbr>
    </Division>
    <Division>
        <Name>Back Office</Name>
        <Abbr>BO</Abbr>
    </Division>
</Divisions>

What I need to do is query this file using the Abbr of the division to get the Name.

I have used the following code to create a XPathNodeIterator:

XPathNodeIterator XPNIDivisions = this.DataSources["Divisions"].CreateNavigator().Select("/Divisions/Division/Name");

So now, how do I search for the name of the division whose Abbr is 'NSS'? Or is the code I'm using wrong (I'm new to XML manipulation)?

Please note that these abbreviations and names could change (more could be added, or some could be deleted); so I need it to be dynamic, meaning that if I have the Abbr in a variable MyAbbr, I have to look for its Name.

Thanks in advance,

Yusuf

Yusuf
  • 409
  • 5
  • 15

2 Answers2

1

Can you use Linq to Xml? I was able to get this to work

string name = XDocument.Load("XMLFile1.xml")
            .Descendants("Division")
            .Where(x => x.Element("Abbr").Value == "NSS")
            .First()
            .Element("Name")
            .Value;
RogerNoble
  • 392
  • 3
  • 13
1

Finally I've been able to find a solution, and I must say it's a bit more complex than Roger's solution; you have to know a bit about XPath expressions.

So what I had to do was just change the select from before

XPathNodeIterator XPNIDivisions = this.DataSources["Divisions"].CreateNavigator().Select("/Divisions/Division/Name");

to

XPathNodeIterator XPNIDivisions = this.DataSources["Divisions"].CreateNavigator().Select("/Divisions/Division[Abbr=\"thevalue\"]");

where thevalue is of course the value you're looking for.

In fact what I did was define a string variable

StrXPathDiv = "/Divisions/Division[Abbr=\"" + thevalue + "\"]";

and then pass it to the Select() method.

Then use the following code to get the value:

if (XPNIDivisions.MoveNext()) //If any record found
{
    XPNIDivisions.Current.MoveToChild(XPathNodeType.Element);
    XPNavMyDivision.SetValue(XPNIDivisions.Current.Value);
}
else { XPNavMyDivision.SetValue(""); }

where XPNavMyDivision is the navigator for where I need to set the value.

Thank you very much for your time and help Roger; I would have tried your solution if I were sure everyone had .NET 3.5 installed; however I'm quite sure of the opposite, so I had to stick with this.

Yusuf
  • 409
  • 5
  • 15
  • Don't forget to escape apostrophes in thevalue - see http://stackoverflow.com/questions/1341847/special-character-in-xpath-query – marapet Aug 18 '10 at 20:37
  • yep, that's what I did here: StrXPathDiv = "/Divisions/Division[Abbr=\"" + thevalue + "\"]"; with the `\"` – Yusuf Aug 19 '10 at 04:38