SOAP XML XPath Navigation Traversal Examples
SOAP responses often contain nested XML structures that require navigation to extract meaningful values. The IPWorks XML and SOAP components provide XPath-based functionality to traverse these structures efficiently.
XML Traversal Using XPath
The following example demonstrates how to navigate an XML document using different XPath expressions and related properties:
Loading Input Data
Xml xml = new Xml();
xml.InputData =
"<root><parent1>" +
"<child1 attr1=\"1\">value 1</child1>" +
"<child2><grandchild1>value 2</grandchild1></child2>" +
"</parent1></root>";
Basic Navigation
xml.XPath = "/root/parent1/child1";
Console.WriteLine(xml.XElement);
xml.XPath = "../child2";
Console.WriteLine(xml.XElement);
Attributes, Parent, Children, Subtree
xml.XPath = "/root/parent1/child1";
Console.WriteLine(xml.XAttributes[0].Value);
Console.WriteLine(xml.XParent);
xml.XPath = "/root/parent1";
foreach (var c in xml.XChildren)
Console.WriteLine(c.Name + ": " + c.XText);
xml.XPath = "/root/parent1/child2";
Console.WriteLine(xml.XSubTree);
Indexing and Attribute Filtering
xml.XPath = "/root/parent1/[1]";
Console.WriteLine(xml.XElement);
xml.XPath = "/root/parent1/[last()]";
Console.WriteLine(xml.XElement);
xml.Reset();
xml.InputData =
"<root><parent>" +
"<child attr1=\"1\">child1</child>" +
"<child attr2=\"2\">child2</child>" +
"</parent></root>";
xml.XPath = "/root/parent/child[@attr1='1']";
Console.WriteLine(xml.XText);
Note: In the SOAP component, XSubTree is a configuration setting instead of a property.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.