load Xpath document

J

JD

Hello-

I am a beginner to .NET XML, and I currently have a web service that returns
a full XML document. I want to use an XPath statement to return a portion
of the same XML file in a new function.

I am trying to perform a SELECTNODES using a XPath statement from the
original XML load, but I cannot seem to write this back out into a XML file.
I realize the error is in sing the XmlNodeList statement, but I am a bit
stumped.

Any ideas?

Here is my code:

Public Function _
GetService() As System.Xml.XmlDocument

Dim myXML As New XmlDocument()

Dim specs As String

specs = "/Root/Bind/Application[1]"

myXML.Load("App_Data/data.xml")

Dim myXmlNodeList As XmlNodeList = myXML.SelectNodes(specs)

myXML.Load(myXmlNodeList)

GetService = myXML

End Function
 
M

Martin Honnen

JD wrote:

I am trying to perform a SELECTNODES using a XPath statement from the
original XML load, but I cannot seem to write this back out into a XML file.
I realize the error is in sing the XmlNodeList statement, but I am a bit
stumped.
Public Function _
GetService() As System.Xml.XmlDocument

It is not clear what you want to achieve. If the return type of the
function is XmlDocument then you can't return an XmlNodeList. You would
either need to manipulate the existing XmlDocument instance to remove
all nodes besides the ones in the node list (keeping the root element
perhaps or adding a new) or you would need to create a new XmlDocument
instance and then for instance use ImportNode to import the nodes from
the node list into the new document (after creating a root element first).
 
J

JD

Martin-

Thanks for the comments. Let me provide more detail on what I am trying to
accomplish:

I currently have a web service that when invoked will produce a XML file
that I have on the server....let's call it File A. I am producing the file
just by using the LOAD command from XMLDocument.

I want to write other functions in the web service that will return subsets
of File A. For example, take a look at the following XML:

<root>
<person>
<name>Jeff</name>
</person>
<place>
<location>Home</location>
</place>
</root>

Let's say that File A is the entire XML above, and I want to provide another
function that will just return everything from the PERSON tag.

I was trying to do this by using an XPath string under SelectNodes, but
maybe I am going about this the wrong way. What you said about writing the
NodeList back to an XML file makes perfect sense, but can I produce the XML
subset through the DOM just by performing a query?

I hope this provides more insight....Any further help is greatly
appreciated.

Thanks

JD
 
M

Martin Honnen

JD wrote:

Let's say that File A is the entire XML above, and I want to provide another
function that will just return everything from the PERSON tag.

I was trying to do this by using an XPath string under SelectNodes, but
maybe I am going about this the wrong way. What you said about writing the
NodeList back to an XML file makes perfect sense, but can I produce the XML
subset through the DOM just by performing a query?

Make the web method return XmlNode as the type and then use XPath and
SelectSingleNode to select whatever node you want to return e.g.
return aXmlDocument.SelectSingleNode("/root/person");

If you use SelectNodes then you have an XmlNodeList and that does
neither fit XmlDocument nor XmlNode. You don't have to write the
XmlNodeList back to a file on the file system but as said, if you want
the web method to return an XmlDocument instance then you need DOM code e.g.
XmlDocument responseXML = new XmlDocument();
responseXML.AppendChild(responseXML.CreateElement("new-root"));
while (xmlNodeListInstance.Count > 0) {
responseXML.DocumentElement.AppendChild(
responseXML.ImportNode(xmlNodeListInstance[0], true)
);
}
return responseXML;
 
J

JD

Martin-

Thanks for the tips. I decided to use the XMLNode method with the
SelectSingleNode(XPATH) return. Works like a charm....

I appreciate the help.

JD
Martin Honnen said:
JD wrote:

Let's say that File A is the entire XML above, and I want to provide
another function that will just return everything from the PERSON tag.

I was trying to do this by using an XPath string under SelectNodes, but
maybe I am going about this the wrong way. What you said about writing
the NodeList back to an XML file makes perfect sense, but can I produce
the XML subset through the DOM just by performing a query?

Make the web method return XmlNode as the type and then use XPath and
SelectSingleNode to select whatever node you want to return e.g.
return aXmlDocument.SelectSingleNode("/root/person");

If you use SelectNodes then you have an XmlNodeList and that does neither
fit XmlDocument nor XmlNode. You don't have to write the XmlNodeList back
to a file on the file system but as said, if you want the web method to
return an XmlDocument instance then you need DOM code e.g.
XmlDocument responseXML = new XmlDocument();
responseXML.AppendChild(responseXML.CreateElement("new-root"));
while (xmlNodeListInstance.Count > 0) {
responseXML.DocumentElement.AppendChild(
responseXML.ImportNode(xmlNodeListInstance[0], true)
);
}
return responseXML;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top