Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

XML & .net

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

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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

ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of ITsolutionWizard

ASKER

Best...
well, same thought posted earlier was not being accepted. well done.