Pomster
Pomster

Reputation: 15197

Get innertext from XML tags?

I am trying to populate an array with the inner text or value of a XML tag. I can loop though the nodes of the tag i am just having an issue pulling out the stored value.

I am looping though the child nodes of the Properties Tag, XML at bottom of Question.

    XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C://Users//Shaun//Documents//Visual Studio 2010//Projects//LightStoneTestService//LightStoneTestService//FileTest//Testdo.xml");

//XmlNodeList dataNodes = xmlDoc.SelectNodes("//Properties");
XmlNodeList oXMlNodeList = xmlDoc.GetElementsByTagName("Properties");

for (int Count = 0; Count < 27; Count++)
{
    foreach (XmlNode node in oXMlNodeList)
    {                    
        int Max = node.ChildNodes.Count;                   
        foreach (XmlNode childNode in node.ChildNodes) //For each child node in FieldData
        {                          
              if (ArrayProperties[Count].LightStoneTag == childNode.Name)
            {
              ArrayProperties[Count].Value = node.SelectSingleNode(ArrayProperties[Count].LightStoneTag).InnerText;    
            }                                                     
        }
    }
}

***** My Xml file looks as follows:

<NewDataSet>
  <RequestData xmlns="RequestData">
    <Req_ID>3204eba5-07f4-4e83-8b46-d89fa1d70bf6</Req_ID>
  </RequestData>
  <Properties xmlns="Properties">
    <sr_id>19374324</sr_id>
    <prop_id>9841107</prop_id>
    <DEED_ID>21</DEED_ID>
    <PROPTYPE_ID>2</PROPTYPE_ID>
    <SS_ID>2315</SS_ID>
    <NAD_ID>3048001</NAD_ID>
    <property_type>SS</property_type>
    <PROVINCE>GA</PROVINCE>
    <MUNICNAME>CITY OF JOHANNESBURG</MUNICNAME>
    <DEEDTOWN>ALLENS NEK</DEEDTOWN>
    <SECTIONAL_TITLE>SS GREENHILLS</SECTIONAL_TITLE>
    <UNIT>15</UNIT>
    <TownShip>ALLENS NEK</TownShip>
    <Purchase_Price>236500</Purchase_Price>
    <Purchase_Date>20031020</Purchase_Date>
    <Bond_Number>SB37369/2006</Bond_Number>
    <Township_alt>ALLEN'S NEK EXT 32</Township_alt>
    <RE>false</RE>
  </Properties>
</NewDataSet>

Upvotes: 1

Views: 1670

Answers (1)

Ivan Golović
Ivan Golović

Reputation: 8832

It's the namespace issue. Add this portion of code before the for loop:

        NameTable nt = new NameTable();
        nt.Add("Properties");
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
        nsmgr.AddNamespace("ns1", "Properties");

Change the line where you retrieve InnerText to:

        ArrayProperties[Count].Value = node
            .SelectSingleNode(
                String.Concat("//ns1:", ArrayProperties[Count].LightStoneTag), 
                nsmgr)
            .InnerText;

Upvotes: 1

Related Questions