简体   繁体   中英

Can I get data from a specific location in an XML file?

I'm trying to assign a value in a location to a string. I can't figure out how to set up the attribute portion of this.

string type = null;

type = xmlNodeComplex4.ParentNode.ParentNode.ParentNode.PreviousSibling.PreviousSibling.PreviousSibling.FirstChild.NextSibling.FirstChild.Attributes["@ID"+ RefID1].FirstChild.NextSibling.NextSibling.FirstChild.InnerText;

This isn't working. The part where I have FirstChild.Attributes["@ID" + RefID1] doesn't work. The code compiles with no problem.. but this line takes a dump and I get an error. I know it is in how I'm trying to access the attribute portion. I've tried many different combinations for the data in the [ ]. Is there a way to do this, if so, can someone direct me in the right direction.

The RefID is a string which contains data like this _154. That FirstChild position has an attribute that looks like this.

<CATALOG>
  <OBJECTS>
    <REFS ID = "_150">
        <CITY>Centerville</CITY>
        <STATE>Ohio</STATE>
        <ZIP>45459</ZIP>
    </REFS>

    <REFS ID = "_154">
        <CITY>Troy</CITY>
        <STATE>Michigan</STATE>
        <ZIP>48083</ZIP>
    </REFS>

  </OBJECTS>
  <PLANT>
     <COMMON>Bloodroot</COMMON>
     <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
     <ZONE>4</ZONE>
     <LIGHT>Mostly Shady</LIGHT>
     <PRICE>$2.44</PRICE>
     <RefID1>_0154</RefID1>

  </PLANT>
</CATALOG>

I saw your comment below. Then you can circle all nodes and when you find the one that you seek with RefID1 -> go in its children and get the type.

XmlNodeList xnList = xml.SelectNodes("/Element[@*]");
foreach (XmlNode xn in xnList)
{
  if(xn.Attributes["ID"].Value == RefID1)
  {
    string type = xn.FirstChild.NextSibling.NextSibling.FirstChild.InnerText;
  }
}

You can do a selection node by tag and get the value of ID attribute.

XmlNode childNode = parentNode.SelectSingleNode("DATA");
if (childNode!= null)
{
   type = childNode.Attributes["ID"].Value;
}

With that tiny snippet of your XML, I can only suggest to use SelectSingleNode() with general XPath like this :

string xpath = String.Format("//DATA[@ID='{0}'", RefID1);
string state = myXmlDocument.SelectSingleNode(xpath).InnerText;

Above XPath means select <DATA> element, at any level within xml document, having attribute ID value equals value of RefID1 .

UPDATE :

Given the updated XML, you can try this way :

string xpath = String.Format("//REFS[@ID='{0}']/STATE", RefID1);
string data = myXmlDocument.SelectSingleNode(xpath).InnerText;

or if you really need the query to be relative to xmlNodeComplex4 :

string xpath = String.Format("./../preceding-sibling::OBJECTS/REFS[@ID='{0}']/STATE", RefID1);
var state = xmlNodeComplex4.SelectSingleNode(xpath).InnerText;

You can see XPath /.. is similar to ParentNode and /preceding-sibling is similar to PreviousSibling .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM