473,721 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlDocumentClas s and Finding Nodes

I am trying to find nodes in an xml document using the XmlDocument class and
having some problems. Everything seems to work OK when there are no encoded
characters (< = &lt;) but when I try to navigate a document in which the
outer nodes are not encoded but the nested nodes are encoded, I can't find
any of the encoded nodes using the XmlDocument selectNodes method. Is there
a simple way to handle this situation? Please advise. Thanks for your
help.

Best Regards,

Paul J. Lay
Nov 12 '05 #1
8 1577
Paul J Lay wrote:
I am trying to find nodes in an xml document using the XmlDocument class and
having some problems. Everything seems to work OK when there are no encoded
characters (< = &lt;) but when I try to navigate a document in which the
outer nodes are not encoded but the nested nodes are encoded, I can't find
any of the encoded nodes using the XmlDocument selectNodes method. Is there
a simple way to handle this situation? Please advise. Thanks for your
help.


Sorry, but there is no such thing as "encoded node". Provide a sample
please.

--
Oleg Tkachenko [XML MVP, MCAD]
http://blog.tkachenko.com
Nov 12 '05 #2
Hi Paul,

I agree with Oleg that the nodes are not encoded when they are loaded to
XmlDocument class. Could you please provide a sample code and Xml fragment?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
I have attached a sample showing the problem in an earlier post today in
this thread. You say that there is no such thing as an encoded node but if
you load any xml document containing encoded tags into the XmlDocument
object class and then convert the document to a string (strXml =
xmlDoc.OuterXml ;) you can easily see the encoded tags in the string. My
problem is that when I am dealing with a document containing both encoded
and non-encoded tags, the SelectNodes method does not see the encoded tags.
Any insight you can provide on this would be greatly appreciated. Thanks
for your help.

Best Regards,

Paul J Lay

"Oleg Tkachenko [MVP]" <oleg@no_!spam! _please!tkachen ko.com> wrote in
message news:ek******** ******@tk2msftn gp13.phx.gbl...
Paul J Lay wrote:
I am trying to find nodes in an xml document using the XmlDocument class
and having some problems. Everything seems to work OK when there are no
encoded characters (< = &lt;) but when I try to navigate a document in
which the outer nodes are not encoded but the nested nodes are encoded, I
can't find any of the encoded nodes using the XmlDocument selectNodes
method. Is there a simple way to handle this situation? Please advise.
Thanks for your help.


Sorry, but there is no such thing as "encoded node". Provide a sample
please.

--
Oleg Tkachenko [XML MVP, MCAD]
http://blog.tkachenko.com

Nov 12 '05 #4
"Paul J Lay" <pj***@msn.co m> wrote in message news:Oj******** *****@TK2MSFTNG P15.phx.gbl...
problem is that when I am dealing with a document containing both encoded and non-encoded tags, the SelectNodes method does not
see the encoded tags. Any insight you can provide on this would be greatly appreciated. Thanks for your help.


To summarize the sample XML document, Paul's document has a ServiceResponse
element that contains, how shall I call it, a "large text object". That large text object
is actually another XML document with any offending angle-brackets replaced by the
character entities, "&lt;" and "&gt;" (among possibly others), i.e.,

- - - simplifiedExamp le.xml
<?xml version="1.0"?>
<ServiceRespons e>
&lt;?xml version="1.0"?& gt;
&lt;ManagedEnti ties&gt;
&lt;-- more stuff here --&gt;
&lt;/ManagedEntities &gt;
</ServiceResponse >
- - -

So the question is: how to access the nodes of the "encoded" document, i.e.,

- - - exampleOnItsOwn .xml
<?xml version="1.0"?>
<ManagedEntitit es>
<!-- more stuff here -->
</ManagedEntitite s>
- - -

Paul, what you're going to have to do is read the text child node of ServiceRequest
into a string, unreplace the substitutions that were made, and then parse that string
into -another- XmlDocument.

XmlNode hsemServiceResp onse = outerDocument.D ocumentElement;
XmlNode serviceResponse = hsemServiceResp onse.FirstChild ;

System.Diagnost ics.Debug.Asser t( "ServiceRespons e" == serviceResponse .LocalName );

XmlDocument innerDocument = new XmlDocument( );
string escapedXml = serviceResponse .InnerText;
string unescapedXml = escapedXml.Repl ace( "&lt;", "<").Replac e( "&gt;", "<");
innerDocument.L oadXml( unescapedXml);

Once you have the DOM tree in this innerDocument, Paul, you can select it's nodes
with XPath and SelectNodes( ) or take other node-based actions upon it.
Derek Harmon
Nov 12 '05 #5
Thanks for Derek's response.

Hi Paul,

I agree with Derek that after reading the escaped content to a string, you
have to use String.Replace to make it back to normal xml document.

Alternatively, you can also put all the data in a CDATA block. Please check
the following link for more information for CDATA.

http://www.w3schools.com/xml/xml_cdata.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #6
Oleg, Derek and Kevin thanks very much for your prompt response and
excellent help. It has allowed me to resolve my problem.

Best Regards,

Paul J Lay

"Kevin Yu [MSFT]" <v-****@online.mic rosoft.com> wrote in message
news:Ru******** ********@TK2MSF TNGXA01.phx.gbl ...
Thanks for Derek's response.

Hi Paul,

I agree with Derek that after reading the escaped content to a string, you
have to use String.Replace to make it back to normal xml document.

Alternatively, you can also put all the data in a CDATA block. Please
check
the following link for more information for CDATA.

http://www.w3schools.com/xml/xml_cdata.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #7


Derek Harmon wrote:

- - - simplifiedExamp le.xml
<?xml version="1.0"?>
<ServiceRespons e>
&lt;?xml version="1.0"?& gt;
&lt;ManagedEnti ties&gt;
&lt;-- more stuff here --&gt;
&lt;/ManagedEntities &gt;
</ServiceResponse >
- - -

So the question is: how to access the nodes of the "encoded" document, i.e.,

- - - exampleOnItsOwn .xml
<?xml version="1.0"?>
<ManagedEntitit es>
<!-- more stuff here -->
</ManagedEntitite s>
- - -

Paul, what you're going to have to do is read the text child node of ServiceRequest
into a string, unreplace the substitutions that were made, and then parse that string
into -another- XmlDocument.

XmlNode hsemServiceResp onse = outerDocument.D ocumentElement;
XmlNode serviceResponse = hsemServiceResp onse.FirstChild ;

System.Diagnost ics.Debug.Asser t( "ServiceRespons e" == serviceResponse .LocalName );

XmlDocument innerDocument = new XmlDocument( );
string escapedXml = serviceResponse .InnerText;
string unescapedXml = escapedXml.Repl ace( "&lt;", "<").Replac e( "&gt;", "<");


I actually think you don't have to do the Replace stuff, indeed you
should not do that as that is going to hurt you, not in the simple
example above but in more complex cases. As for the simple example, the
InnerText (e.g. the escapedXml variable) simply contains the string

<?xml version="1.0"?>
<ManagedEntitie s>
<-- more stuff here -->
</ManagedEntities >

so there are not any entity/character references as the DOM has already
made sure you get a proper string. So in that case your Replace is not
necessary and does not improve things.

Now imagine the original document is something like

<?xml version="1.0" encoding="UTF-8"?>
<text>&lt;&gt ;</text>

which could be wrapped up as text in another document as

<root>&lt;?xm l version="1.0"?& gt;
&lt;text&gt;&am p;lt;&amp;gt;&l t;/text&gt;
</root>

where the InnerText of the <root> element is then

<?xml version="1.0"?>
<text>&lt;&gt ;</text>

and in that case your Replace attempts on those entity/character
references whould change that string to

<?xml version="1.0"?>
<text><></text>

which would change the semantics and would not parse.

Thus don't do the replace stuff, simply access the content of the
element as a string (e.g. InnerText) and then feed that directly to the
XML parser.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #8
You're welcome, Paul.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #9

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

Similar topics

3
4966
by: Jorn W Janneck | last post by:
hello everyone. i have the sort of question that makes me feel like i am missing the forest for the trees, so apologies if i am missing the blatantly obvious here. i am using saxon, and mostly xslt version 1.1 (the unofficial one). i have a template parameter, say v, which contains a sequence of nodes of the same kind, say A, which all have an attribute, say n. these attributes may have different numeric values. i have identified the...
3
1740
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child> <Child>Name</Child> <Child>Peter</Child>
4
4726
by: Porthos | last post by:
Hi All, I'm trying to find the minimum value of a set of data (see below). I want to compare the lengths of these attribute values and display the lowest one. This would be simple if I could re-assign values to a variable, but from what I gather I can't do that. How do I keep track of the lowest value as I loop through? My XSL document only finds the length of each string and prints it out (for now). I can write a template
2
1418
by: mlybarger | last post by:
i'm trying to use xpath to find nodes that don't match any other nodes (of a different type). here's an example xml: <a> <b> <c>one</c> <c>three</c> <d> <e>one</e>
1
1904
by: mark.engelberg | last post by:
I am having trouble identifying the source of a memory leak in a Windows Python program. The basic gist is as follows: 1. Generate a directed graph (approx. 1000 nodes). 2. Write the graph to a file. 3. Use the os.system command to invoke another program which processes the graph file (graphViz), and generates a gif image of the graph. 4. Use another os.system command to delete the intermediate file, which is no longer needed. 5. Append...
2
10700
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
6
7078
by: Jay Bienvenu | last post by:
I have the following Visual Basic .NET code in an ASP.NET project: ' ... Dim xdStructure As XmlDocument Dim xnStructureRoot As XmlNode xdStructure = New XmlDocument xdStructure.Load(Server.MapPath("~/xml/Structure.xml"))
6
2726
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any further results then the XML document contains a URL to call and get the next set of 10 results. So if there were 25 results total I would get the first 10 in the first XML document, then after making a second request I would get results 11 to 20...
1
1820
by: renfrochris | last post by:
I'm having difficulty finding the correct syntax that will allow me to select a group of invoices based on the total of an amount column located in its line items. Below are simplified examples of my XML and XSLT files: XML FILE <?xml version="1.0" standalone="yes"?> <?xml-stylesheet type="text/xsl" href="OutstandingInvoiceBalances.xslt"?>
0
8834
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9127
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8002
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4483
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.