How to search for a specific tag in XML using C# [ Microsoft Infopath]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Knibitz
    New Member
    • Jul 2010
    • 1

    How to search for a specific tag in XML using C# [ Microsoft Infopath]

    I'm a web application student and I'm working to solve an issue pulling in some tags from different XML documents for an Info Path Project I'm working on for a client.

    I'm trying to pull in
    Code:
    <tagX>123</tagX>
    but in my code there's many "TagX"s and I need to pick a specific one.

    The way I'm supposed to find it is direct my program to pull that specific one based on what is encased in.


    I want look inside
    Code:
     <Casing3></Casing3>
    to get my tag.


    Basically it looks something like this:

    Code:
    <Casing1>
     <TagX>
       FALSE
     </TagX>
    <Casing1>
    
    *lines and lines of code*
    
    <Casing2>
     <TagX>
       FALSE
     </TagX>
    </Casing2>
    
    *lines and lines of code*
    
    <Casing3>
     <TagX>
       123
     </TagX>
    </Casing3>

    I need to point it to "Casing3" specifically.
    I can't just use a continue statement to skip the first two, because there could be none or their could be 7 other "casings" before the "Casing3" in these different XML files.

    I'm pulling in other tags that only occur once, and this is what the code looks like for them (this is just a segment of the working code. It's not clean or perfect, but so far it works)


    Code:
      {
                XPathNavigator mainDSNav = MainDataSource.CreateNavigator();
    
                string s = mainDSNav.SelectSingleNode("/my:myFields/my:XMLFile", NamespaceManager).Value; //reads xml file
    
                if (s == "")
                {
                }
                else
                {
                    string result = Encoding.ASCII.GetString(InfoPathAttachmentDecoder(s));
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(result);
                    XmlNodeList list = doc.GetElementsByTagName("CAT");  //gets node "CAT" from the XML file
                    XmlNodeList list2 = doc.GetElementsByTagName("DOG"); //gets node "DOG" from the XML file
    
                    foreach (XmlNode node in list)
                    {
                        mainDSNav.SelectSingleNode("/my:myFields/my:catText", NamespaceManager).InnerXml += node.InnerText; //sends the value encased in node "CAT" to the "catText" field
                    }
                    foreach (XmlNode node in list2)
                    {
                        mainDSNav.SelectSingleNode("/my:myFields/my:dogText", NamespaceManager).InnerXml += node.InnerText; //sends the value encased in node "DOG" to the "dogText" field
                    }
               
                }
    
            }
    ***tag names were edited to protect my client***

    Any ideas?

    (and if I'm using terms wrong, I apologize. This is my first project working with XML, so I'm learning as I go)
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    SelectSingleNod e("//Casing3/TagX")

    Comment

    Working...