473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

1 space node returns as empty

Hi;

This code is in J# so it looks a little weird but you could change it to C#
in a couple of seconds.

The problem here is a single space node returns as an empty node:

public static void main(String[] args) throws Exception
{

XmlDocument doc = new XmlDocument();
doc.set_Preserv eWhitespace(tru e);

String xml = "<root><spa ce> </space></root>";
Encoding encoder = Encoding.GetEnc oding("utf-8");
doc.Load(new MemoryStream(en coder.GetBytes( xml)));

XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc)).CreateNavi gator();
nav = nav.SelectSingl eNode("/root");
nav = nav.SelectSingl eNode("./space");
String str = nav.get_Value() ;
int len = str.length();
System.out.prin tln("value = {" + str + "}");
System.out.prin tln("length = " + len);
}

Any ideas how to get back the space that is the node value? I tried
doc.set_Preserv eWhitespace(tru e); both before and after the Load.
--
thanks - dave
Nov 12 '05 #1
7 1907
Hi dave,

I don't quite understand the code. Does XpathNavigator has a method named
SelectSingleNod e?

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

Nov 12 '05 #2
Hello;

Yes, new to .net 2.0.

http://msdn2.microsoft.com/en-us/library/5htcxk90

--
thanks - dave
"Kevin Yu [MSFT]" wrote:
Hi dave,

I don't quite understand the code. Does XpathNavigator has a method named
SelectSingleNod e?

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

Nov 12 '05 #3


David Thielen wrote:

The problem here is a single space node returns as an empty node:

public static void main(String[] args) throws Exception
{

XmlDocument doc = new XmlDocument();
doc.set_Preserv eWhitespace(tru e);

String xml = "<root><spa ce> </space></root>";
Encoding encoder = Encoding.GetEnc oding("utf-8");
doc.Load(new MemoryStream(en coder.GetBytes( xml)));

XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc)).CreateNavi gator();
nav = nav.SelectSingl eNode("/root");
nav = nav.SelectSingl eNode("./space");
String str = nav.get_Value() ;
int len = str.length();
System.out.prin tln("value = {" + str + "}");
System.out.prin tln("length = " + len);
}


When I try the following C#

string xmlMarkup = "<root><tex t> </text></root>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Pre serveWhitespace = true;
xmlDocument.Loa dXml(xmlMarkup) ;

XmlElement text = xmlDocument.Sel ectSingleNode(@ "/root/text") as
XmlElement;
if (text != null) {
Console.WriteLi ne("InnerText: '{0}', Length: {1}.",
text.InnerText, text.InnerText. Length);

XPathNavigator xPathNavigator = text.CreateNavi gator();
Console.WriteLi ne("Value: '{0}', Length: {1}",
xPathNavigator. Value, xPathNavigator. Value.Length);
}

with .NET 2.0 Beta 2 I get the output

InnerText: ' ', Length: 1.
Value: ' ', Length: 1

which seems fine to me, so maybe you can streamline your code.

Or do you have specific needs to
- create a memory stream from a string
- load an XmlDocument from that memory stream
- create a XmlNodeReader on that XmlDocument
- create an XPathDocument on that XmlNodeReader
- create an XPathNavigator from the XPathDocument
when loadXml exists for the first two steps and the XPathNavigator can
simply be created from an XmlNode?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4
Hi;

Interesting - I tried that and got that too (J#):
XmlElement elem = (XmlElement)doc .SelectSingleNo de("/root/space");
String str = elem.get_InnerT ext(); // get a 1 space string

XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc)).CreateNavi gator();
nav = nav.SelectSingl eNode("/root/space");
String str = nav.get_Value() ; // a 0 length string

Does XPathDocument just never allow whitespace? I hate to not be able to use
it as it is the recomended API and a lot faster. But I have to get the actual
node contents.

--
thanks - dave
"Martin Honnen" wrote:


David Thielen wrote:

The problem here is a single space node returns as an empty node:

public static void main(String[] args) throws Exception
{

XmlDocument doc = new XmlDocument();
doc.set_Preserv eWhitespace(tru e);

String xml = "<root><spa ce> </space></root>";
Encoding encoder = Encoding.GetEnc oding("utf-8");
doc.Load(new MemoryStream(en coder.GetBytes( xml)));

XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc)).CreateNavi gator();
nav = nav.SelectSingl eNode("/root");
nav = nav.SelectSingl eNode("./space");
String str = nav.get_Value() ;
int len = str.length();
System.out.prin tln("value = {" + str + "}");
System.out.prin tln("length = " + len);
}


When I try the following C#

string xmlMarkup = "<root><tex t> </text></root>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Pre serveWhitespace = true;
xmlDocument.Loa dXml(xmlMarkup) ;

XmlElement text = xmlDocument.Sel ectSingleNode(@ "/root/text") as
XmlElement;
if (text != null) {
Console.WriteLi ne("InnerText: '{0}', Length: {1}.",
text.InnerText, text.InnerText. Length);

XPathNavigator xPathNavigator = text.CreateNavi gator();
Console.WriteLi ne("Value: '{0}', Length: {1}",
xPathNavigator. Value, xPathNavigator. Value.Length);
}

with .NET 2.0 Beta 2 I get the output

InnerText: ' ', Length: 1.
Value: ' ', Length: 1

which seems fine to me, so maybe you can streamline your code.

Or do you have specific needs to
- create a memory stream from a string
- load an XmlDocument from that memory stream
- create a XmlNodeReader on that XmlDocument
- create an XPathDocument on that XmlNodeReader
- create an XPathNavigator from the XPathDocument
when loadXml exists for the first two steps and the XPathNavigator can
simply be created from an XmlNode?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #5
David Thielen wrote:
Any ideas how to get back the space that is the node value? I tried
doc.set_Preserv eWhitespace(tru e); both before and after the Load.


When loading XPathDocument you have to preserve whitespace, by default
it's not preserved.
XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc), XmlSpace.Preser ve).CreateNavig ator();

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #6
David Thielen wrote:
Does XPathDocument just never allow whitespace? I hate to not be able to use
it as it is the recomended API and a lot faster. But I have to get the actual
node contents.


Second argument to XPathDocument is supposed to control whitespace
handling. By default it's XmlSpace.Defaul t, which doesn't preserve
insignificant whitespace (whitespace-only text between tags).
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #7
Bingo - that's it.

I never looked at that ctor because I was using the XPathDocument(S tream)
ctor. But doing it as you suggest below works & slips right in.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
Any ideas how to get back the space that is the node value? I tried
doc.set_Preserv eWhitespace(tru e); both before and after the Load.


When loading XPathDocument you have to preserve whitespace, by default
it's not preserved.
XPathNavigator nav = new XPathDocument(n ew
XmlNodeReader(d oc), XmlSpace.Preser ve).CreateNavig ator();

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

Nov 12 '05 #8

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

Similar topics

2
2239
by: Malcolm Dew-Jones | last post by:
I am looking at xslt 1.0 and trying to understand if empty text nodes are supposed to be stripped or not as the default behaviour. 3.4 starts by listing rules for when white space is not stripped and then says "Otherwise the text node is stripped". which appears to contradict a later paragraph that discusses the details of the selection of nodes to be stripped ("Initially ... preserve ... all element names").
4
62398
by: n_o_s_p_a__m | last post by:
My xml doc has many <title></title> and <title> in it, meaning the nodes have no content (although some do). How can I test for this? I tried title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) I tried //title (doesn't work) Any suggestions welcome.
3
1204
by: chris yoker via DotNetMonster.com | last post by:
hi, I have an xmlFile <code> <rows> <row> <PRODUCT-TYPE>bike</PRODUCT-TYPE> <PRODUCT-DATE>01/01/2004</PRODUCT-DATE> <ADDED-NODE>"blah"<ADDED-NODE /> </row>
0
1701
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg., <Root> <mysection>
2
1360
by: Todd Price | last post by:
I'm trying to use XPath to get a handle on one specific node so that I can replace it with an xml fragment I've loaded from another file. So if I have this XML: <issue> <id>1</id> <date>7/28/2006</date> <states/> </issue>
1
1645
by: robert | last post by:
I have an xmldocument and need to set the preservewhitespace to true as after processing the document I want to write out the xml (using outerxml/innerxml) and have the identations preserved for readability. However with the preservewhite space option set code such as: foreach (XmlNode node in ParentNode) { }
10
2083
by: Phil Stanton | last post by:
I have a table of SpaceAreas eg Food Store, Garden Shed etc with the first and last bin for each Space Area defined. eg Food Store First Space 1, last space 26 and Gargen Shed First space 1, last Space 50. SpaceAreas Table is SpaceAreaID Auto SpaceAreaDesc Text FirstSpace Integer LastSpace Integer I have a secont table of the Equipment Stored in each space
4
8775
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; If I have an XPathNavigator object and for a given xpath statement need to know if the node exists, how should I do this? I have found for some xpath functions it returns an empty string if the node does not exist but that is also the result for an empty node. -- thanks - dave david_at_windward_dot_net
5
6226
by: mostro713 | last post by:
Hello all, I would like to write a script in Python to email me when disk space gets below a certain value. My first question (I'm sure of many) is how do get this output into a dictionary or list to index the values? import os os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
0
8826
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...
1
8503
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
7330
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...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
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...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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.