Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XML & .net

Posted on 2016-12-01
5
Medium Priority
?
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 53

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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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
 
LVL 1

Author Closing Comment

by:ITsolutionWizard
ID: 41909759
Best...
0
 
LVL 53

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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