473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

XPathDocument and retrieving xml string

I load an xml stream into a xpathdocument type, compile a xpath expression
and select to specific node. I find the node and wish to return the innerxml
of that node. When I use value of the current node I get back the "values"
of the child nodes but no formatted xml which I need for transformations
later in the sequence. Is there a way to pull pure xml from the
xpathdocument?
Nov 12 '05 #1
2 11916


Robert Strickland wrote:
I load an xml stream into a xpathdocument type, compile a xpath expression
and select to specific node. I find the node and wish to return the innerxml
of that node. When I use value of the current node I get back the "values"
of the child nodes but no formatted xml which I need for transformations
later in the sequence. Is there a way to pull pure xml from the
xpathdocument?


If you need access to the nodes as XmlNode objects then you need an
XmlDocument and not an XPathDocument, for instance try
using System;
using System.Xml;
using System.Xml.XPath;

public class Test20031228 {

public static void Main (string[] args) {
XPathDocument xpathDocument = new XPathDocument(@"test20031228.xml");
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
TestXPathNavigator(xpathNavigator);

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20031228.xml");
xpathNavigator = xmlDocument.CreateNavigator();
TestXPathNavigator(xpathNavigator);
}

static void TestXPathNavigator (XPathNavigator xpathNavigator) {
XPathExpression xpathExpression = xpathNavigator.Compile("/*");
XPathNodeIterator nodeIterator =
xpathNavigator.Select(xpathExpression);
while (nodeIterator.MoveNext()) {
xpathNavigator = nodeIterator.Current;
IHasXmlNode hasXmlNode = xpathNavigator as IHasXmlNode;
if (hasXmlNode != null) {
XmlNode currentNode = hasXmlNode.GetNode();
Console.WriteLine(currentNode.InnerXml);
}
else {
Console.WriteLine("Can't access nodes.");
}
}
}

}

and you will find that the cast to IHasXmlNode (which has the GetNode
method) fails for the navigator created from the XPathDocument while it
works for the navigator created from the XmlDocument.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2
Thanks

"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...


Robert Strickland wrote:
I load an xml stream into a xpathdocument type, compile a xpath expression and select to specific node. I find the node and wish to return the innerxml of that node. When I use value of the current node I get back the "values" of the child nodes but no formatted xml which I need for transformations
later in the sequence. Is there a way to pull pure xml from the
xpathdocument?


If you need access to the nodes as XmlNode objects then you need an
XmlDocument and not an XPathDocument, for instance try
using System;
using System.Xml;
using System.Xml.XPath;

public class Test20031228 {

public static void Main (string[] args) {
XPathDocument xpathDocument = new XPathDocument(@"test20031228.xml");
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
TestXPathNavigator(xpathNavigator);

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20031228.xml");
xpathNavigator = xmlDocument.CreateNavigator();
TestXPathNavigator(xpathNavigator);
}

static void TestXPathNavigator (XPathNavigator xpathNavigator) {
XPathExpression xpathExpression = xpathNavigator.Compile("/*");
XPathNodeIterator nodeIterator =
xpathNavigator.Select(xpathExpression);
while (nodeIterator.MoveNext()) {
xpathNavigator = nodeIterator.Current;
IHasXmlNode hasXmlNode = xpathNavigator as IHasXmlNode;
if (hasXmlNode != null) {
XmlNode currentNode = hasXmlNode.GetNode();
Console.WriteLine(currentNode.InnerXml);
}
else {
Console.WriteLine("Can't access nodes.");
}
}
}

}

and you will find that the cast to IHasXmlNode (which has the GetNode
method) fails for the navigator created from the XPathDocument while it
works for the navigator created from the XmlDocument.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ian Lawton | last post by:
Hi, in a C# app, I have an XPathDocument that looks similar to this: <row uniqueid="4234" /> <row uniqueid="4365" /> <row uniqueid="3124" /> <row uniqueid="9879" /> <row uniqueid="1332" />...
5
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: ...
2
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and...
8
by: Keith Chadwick | last post by:
My problem is this. I have to create several xmlReader objects each retrieving 'for xml' formatted sql server data. I then need to peice them together into a single document and place them into a...
2
by: Keith Chadwick | last post by:
I have a merged dataset that contains xml read from SQL Server. I need to place the data into an XPathDocument. I can do the following: mydataset.writeXML("mydata.xml") dim xpdoc as new...
2
by: Jim Lewis | last post by:
After everything I have read and some of my own testing I am convinced that XPathDocument are more efficient if you are only using XML for read only and not modifying the XML. However, I have been...
3
by: Atul | last post by:
Hi, I am getting XmlException while running the following code: string sConfigXPath = Constants.CONST_CONFIG_XPATH + ""; string sConfigFile = Constants.CONST_CONFIG_FILE_LOCATION; //Returns...
0
by: Joe Pannone | last post by:
I need a way to allow a variable to equal an xml document. When using xpathdocument the parameter is asking for a filename. Since I plan on doing a lot of search in the xml document I would like...
1
by: C#Schroeder | last post by:
First off I want to thank all that have helped me in the past. I am new to working with XML in .Net. I have another problem right now with my code. Background: I am trying to create a HTML...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.