0

I have this snippt of XML

<unit class="xxx.xxx.xxx" id="382">
      <customId>000</customId>
      <description>kg</description>
      <key>22452</key>
      <Description>Kilogramm</Description>
 </unit>

how to get the node 'unit' or parnet of the key element using the value of an element. For instance

i have the value of key element above [22452] and it's Uniqe inside the xml-File.

what i am trying to do getting value of customid [000] of that specific tag.

what i did:

var doc = new XmlDocument();
doc.Load(stream); // stream from xml-file
var key = doc.SelectSingleNode(//key/[text()='" + 22452+ "']");  // that i am not sure about it.
var customId = key.InnerText("customId");
5
  • Close. If you have the "key"-Node, you need to navigate to its parent and from there to the "customId" child. Commented Apr 8, 2021 at 13:39
  • yes but this here doesn't work to get the parnet tag => doc.SelectSingleNode(//key/[text()='" + 22452+ "']"); that was just a try Commented Apr 8, 2021 at 13:45
  • above when code executed to get the key i get exception Message "Expression must evaluate to a node-set." Commented Apr 8, 2021 at 13:48
  • 1
    Have a try with var key = doc.SelectSingleNode("//key[contains(., '22452']); Commented Apr 8, 2021 at 13:54
  • perfect that's work, you save my ass from implement a lot lines of code! Commented Apr 8, 2021 at 14:16

2 Answers 2

3

For this kind of query you could either find the node and than navigate to the parent.

Or use XPath:

var unitElemnt = doc.SelectSingleNode("//unit[key = '22452']");

(Assuming I've remembered the XPath to match an element's text content correctly.)

This gets a reference to the <unit> element, by using a relative path to the <key> element in the predicate of the XPath expression.

Generally better to avoid // in XPath for performance, but would need full document structure to do that.

Sign up to request clarification or add additional context in comments.

Comments

1

For this you can use Linq to Xml queries.

XElement units = XElement.Load("./Test.xml");

XElement unit = (from item in units.Descendants("unit")
                 where item.Element("key").Value == "22455"
                 select item).FirstOrDefault();

string customId = unit.Element("customId").Value;

supposing your xml file look like :

<?xml version="1.0" encoding="utf-8"?>
<units>
  <unit class="xxx.xxx.xxx" id="385">
    <customId>003</customId>
    <description>kg</description>
    <key>22455</key>
    <Description>Kilogramm</Description>
  </unit>
  <unit class="xxx.xxx.xxx" id="386">
    <customId>004</customId>
    <description>kg</description>
    <key>22456</key>
    <Description>Kilogramm</Description>
  </unit>
</units>

for more reading check Microsoft Linq to Xml Docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.