Groups | Blog | Home
all groups > dotnet xml > march 2006 >

dotnet xml : Read XML document from Web Service


Jeff Ditty
3/15/2006 9:50:04 PM
Hello-

I am looking for the best way to run different Xpath expressions from a web
service, and return the appropriate XML data. The data source that the web
service queries is an XML file, that is not very large (less than 2 MB).

I can retrieve the entire XML file, but I would like to have separate
functions in the web service to pick out certain elements of the file and
basically filter the XML data. There are different methods for invoking
this action (XMLreader, XMLSelectNode, etc...). Can anyone provide some
guidance for the easiest method? Any examples would be most appreciated.

Thanks

agapeton NO[at]SPAM gmail.com
3/22/2006 7:49:13 AM
Here's something I wrote last week to work with XML messages
manually... I give it the xml and what I'm looking for. I expose this
to VB clients so they can access any property they want to in the web
service response message.

using System;
using System.Xml;

namespace XmlSupport
{
public static class XmlSearch
{
private static string FindLeaf(XmlNode node, string leaf) {
foreach (XmlNode child in node.ChildNodes) {
if (child.NodeType == XmlNodeType.Element) {
string returnValue = FindLeaf(child, leaf);
if (!String.IsNullOrEmpty(returnValue)) {
return returnValue;
}

}
else {
if (node.Name == leaf) {
if
(!String.IsNullOrEmpty(node.ChildNodes[0].Value)) {
return node.ChildNodes[0].Value;
}
else {
return String.Empty;
}
}
}
}

return String.Empty;
}

public static string SearchTree(string xml, string leaf) {
XmlDocument xmlDocument = new XmlDocument( );
xmlDocument.LoadXml(xml);

foreach (XmlNode node in xmlDocument.ChildNodes) {
string returnValue = FindLeaf(node, leaf);
if (!String.IsNullOrEmpty(returnValue)) {
return returnValue;
}
}

return String.Empty;
}
}
}

David Betz
WinFX Harmonics Blog
http://www.davidbetz.net/winfx/
AddThis Social Bookmark Button