Solved

XML & .net

Posted on 2016-12-01
5
16 Views
Last Modified: 2016-12-01
I have super simple xml and below .net c# codes and just try to read

stepID=1 and controlID=state and it should return two states under <Data></Data> but it does not happen. not sure why and need experts helping.

Thanks

  public string GetOptionList(string stepID, string controlID)
        {
            string tmpStr = string.Empty;
            XmlDocument myXml = new XmlDocument();
            myXml.Load(Server.MapPath("~") + "\\testStructure.xml");
            XmlNodeList oXmlNodeList = myXml.SelectNodes("Structure/Step[@ID=" + stepID + "]/Section/Control[ID=" + controlID + "]/Data");
            foreach (XmlNode x in oXmlNodeList)
            {
                string option = x.SelectSingleNode("Option").InnerText;
                tmpStr = tmpStr + "<option>" + option + "</option>";
            }
            return tmpStr;
        }

<?xml version="1.0" encoding="utf-8" ?>
<Structure>
    <Step ID="1">
      <Section>
        <Control>
          <Label>First Name</Label>
          <ID>firstName</ID>
          <Type>Text</Type>
          <Cols>4</Cols>
          <Data>
            <Option>None</Option>
          </Data>
          <Required>No</Required>          
         </Control>
        <Control>
          <Label>Last Name</Label>
          <ID>lastName</ID>
          <Type>Text</Type>
          <Cols>4</Cols>
          <Data>
            <Option>None</Option>
          </Data><Required>No</Required>
        </Control>
        <Control>
          <Label>State</Label>
          <ID>state</ID>
          <Type>Select</Type>
          <Cols>4</Cols>
          <Data>
            <Option>CA</Option>
            <Option>TX</Option>
          </Data><Required>No</Required>
        </Control>
        <Control>
          <Label>Do You Like Me?</Label>
          <ID>LikeIt</ID>
          <Type>Select</Type>
          <Cols>4</Cols>
          <Data>
            <Option>Yes</Option>
            <Option>No</Option>
          </Data>
          <Required>No</Required>
        </Control>        
      </Section>
    </Step>
    <Step ID="2">
      <Section>
        <Control>
          <Label>First Name</Label>
          <ID>firstName</ID>
          <Type>Text</Type>
          <Cols>4</Cols>
          <Data>
            <Option>None</Option>
          </Data><Required>No</Required>
        </Control>                  
      </Section>
    </Step>
    <Step ID="3">
      <Section>
        <Control>
          <Label>First Name</Label>
          <ID>firstName</ID>
          <Type>Text</Type>
          <Cols>4</Cols>
          <Data>
            <Option>None</Option>
          </Data><Required>No</Required>
        </Control>
        <Control>
          <Label>Last Name</Label>
          <ID>lastName</ID>
          <Type>Text</Type>
          <Cols>4</Cols>
          <Data>
            <Option>None</Option>
          </Data><Required>No</Required>
        </Control>
        <Control>
          <Label>State</Label>
          <ID>state</ID>
          <Type>Select</Type>
          <Cols>4</Cols>
          <Data>
            <Option>CA</Option>
            <Option>TX</Option>
          </Data><Required>No</Required>
        </Control>
      </Section>
  </Step>
</Structure>

Open in new window

0
Comment
Question by:ITsolutionWizard
5 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
try this:
public string GetOptionList(string stepID, string controlID)
        {
            string tmpStr = string.Empty;
            XmlDocument myXml = new XmlDocument();
            myXml.Load(Server.MapPath("~") + "\\testStructure.xml");
            XmlNodeList oXmlNodeList = myXml.SelectNodes("Structure/Step[@ID=" + stepID + "]/Section/Control[ID='" + controlID + "']/Data/Option");
            foreach (XmlNode x in oXmlNodeList)
            {
                string option = x.InnerText;
                tmpStr = tmpStr + "<option>" + option + "</option>";
            }
            return tmpStr;
        }

Open in new window

0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
Hi,

Pls Try....

private void Form1_Load(object sender, EventArgs e)
        {
            string tmpStr = string.Empty;
            XmlDocument myXml = new XmlDocument();
            
            myXml.Load("E:\\pawan2.xml");
            XmlNodeList oXmlNodeList = myXml.SelectNodes("Structure/Step[@ID=" + "1" + "]/Section/Control[Label='State']");
            foreach (XmlNode x in oXmlNodeList)
            {
                XmlNodeList y = x.SelectNodes("Data/Option");
                foreach (XmlNode z in y)
                {
                    string option = z.InnerText;
                    tmpStr = tmpStr + "<option>" + option + "</option>";
                }                                    
            }
            Console.WriteLine(tmpStr);
        }

Open in new window



Output


EE
Hope it helps !!
1
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
Comment Utility
I think it's XPATH is incorrect it should be something like this:

/Structure/Step[@ID='1']/Section/Control[ID='firstName']/Data/Option

Also single quotes for "stepID" are missing I corrected it below.

Change the following line to

   XmlNodeList oXmlNodeList = myXml.SelectNodes("/Structure/Step[@ID='"+ stepID + "']/Section/Control[ID='" + controlID + "']/Data/Option");

Open in new window

0
 

Author Closing Comment

by:ITsolutionWizard
Comment Utility
Best...
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
well, same thought posted earlier was not being accepted. well done.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now