Solved

XML & .net

Posted on 2016-12-01
5
47 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 50

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 28

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 50

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 35
VB.NET 2008 Publish Error 2 27
No Data for DropDown List 2 26
Need help with a Crystal Report in my C#.Net application 2 16
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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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