views:

1353

answers:

7

I'm new to Web Services and XML and was tasked to parse an XML response packet returned.

What's the best way to parse an XML result in C#.NET?

I need to bind to a data grid as the end result from a search query.

+3  A: 

If you have access to the wsdl for the webservice, there is a utility wsdl that will generate the needed classes and deserialization to call a webservice and parse its response into those classes.

Using the example webservice at w3schools, you would just run this command line:

wsdl "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"

That would generate a file TempConvert.cs that you could add into your project. Then calling the webservice is just a matter of calling the generated functions:

Double fahrenheit = 32.2;
TempConvert tempConverter = new TempConvert();
string Celcius = tempConverter.FahrenheitToCelsius(fahrenheit.ToString());

That TempConvert class takes care of the details of building an XML query, contacting the webservice, and parsing the reply back in. This is a super-simple example, so you only get a string back, but since your webservice is likely to be a little more complicated, you'll get back more complicated classes containing the data you want in a parsed form. Depending on the complexity of the web service, this could even be as simple as an array of strings, or as complex as a giant class heirarchy.

Eclipse
A: 

There are lots of different ways to do this, you could deserialize the xml into an object, use LINQ-to-XML to extract the data, load the XML into a DOM and xpath the data out, etc.

Do you have a XSD for the response packet?

Andrew Hare
A: 

The most common way to parse XML in C# is to use the XMLDocument in System.XML.

However, if the web service is SOAP-based, the easiest method to consume it is to add a reference to the service to your project and let the wizard generate the parsing code for you.

Jekke
A: 

add: using System.Xml

As you peruse the structure of XML, you will see elements in each other (see object XmlElement and XmlNode). Each has child object collections (XmlNode.ChildNodes[]).

XPath is a way to navigate to a specific node or subset of nodes. For a basic overview, read this article.

tsilb
+1  A: 

If dose not have a WSDL you can use the library found under

System.Xml

You can also use LINQ you can use the

System.Xml.Linq

Namespace for LINQ binding.

You will need to look at the packet and figure out what the scheme is.

nlaq
Note that the supplied link points to .NET 1.1, though on the page should be a link to the newer versions.
OregonGhost
+2  A: 

There is a fundamental thing you should know about XML parsing before deciding which technique is best.

With XMLDocument kind of objects the entire XML is loaded into memory where as with a reader object it is a stream, only the chunk that is being processed is loaded to memory. Obviously the reader uses little memory but will not give you the ability to go back/ ahead of the current node being processed. Whereas you have lot of freedom with the doc though the downside is there is a copy of the xml file in memory.

Accordingly if the size of xml is expected to be large, then XmlDocument will not be optimal. What kind of logic you apply using the xml will also affect the choice.

Sesh
Good answer, Sesh! You have stated some of the very reasons I shy away from XmlDocuments. I almost always work with XPathDocument/XPathNavigator objects which are much faster, though maybe not as intuitive as the XmlDocument.
Cerebrus
A: 

@LB: You have not mentioned what is the purpose of parsing the XML returned. If you are performing some modifications in the XML or querying it for some specific data, I would understand, but if you want to simply load it so that it can be displayed in a Datagrid, then I believe that you have two very straightforward (and conventionally used) options :

  • Directly bind the DataGrid to the XML, or use a XmlDataSource as an intermediary.
  • Load the XML into a DataSet and bind your DataGrid to that Dataset.

Personally, I have never liked converting XML to Datasets, unless I have the time to create a specific schema for the XML document. So, I lean towards the first option.

Cerebrus