Solved

XML & .net

Posted on 2016-12-01
5
40 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
ID: 41909721
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 24

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41909726
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
ID: 41909731
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
ID: 41909759
Best...
0
 
LVL 49

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 50
Need to pass stored proc parameters in list 13 32
Why Does This Page Not Show Up? 4 22
VB.Net How to Exit Sub - Exit Form??? 5 50
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

20 Experts available now in Live!

Get 1:1 Help Now