I'm having trouble parsing the xml returned from a web service because it's really just a string. The web service doesn't contain any method to submit the reque开发者_运维知识库st, nor an object to handle the response, so I'm just getting the xml as a string and trying to parse it to properties in an object I've created.
I was messing around with XPath, but I'm unable to figure out how to use a string, or an XmlDocument object with Xpath. I don't have an actual xml file, just a string that I've used to create an XmlDocument object.
private void SetProperties(string _xml)
{
XmlDocument _doc = new XmlDocument();
_doc.LoadXml(_xml);
}
Any ideas as to how I can query that XmlDocument object with XPath?
SelectNodes or SelectSingleNode is a good place to start. There are examples on those pages of selecting/querying node lists from an XmlDocument.
Have you tried calling the various methods on the XmlDocument object? For example, the SelectSingleNode method takes an xpath string and returns an xmlNode.
Also, check this site for additional information: http://www.w3schools.com/
You can create a new XPathExpression object which you then do a select against the XDoc with it. Adding onto the XDocument load code you started with:
XmlDocument _doc = new XmlDocument();
_doc.LoadXml(_xml);
XPathNavigator navigator = _doc.CreateNavigator();
XPathExpression expression = navigator.Compile("/foo/bar");
XPathNodeIterator iterator = navigator.Select(expression);
while (iterator.HasNext()) {
//Do Something With iterator.Current.Value;
}
精彩评论