sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
David Thielen's Avatar

XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s


Question posted by: David Thielen (Guest) on November 12th, 2005 05:09 AM
Hi;

I have an element:
<space> </space>

When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not
a String containing 1 space.

Any ideas?

--
thanks - dave
12 Answers Posted
Kevin Yu [MSFT]'s Avatar
Guest - n/a Posts
#2: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hi dave,

You can set PreserveWhitespace to true, so that the space will be preserved.

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml("<space> </space>");
XmlNode n = doc.SelectSingleNode("space");
MessageBox.Show(n.InnerXml.Length.ToString());

HTH.

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

David Thielen's Avatar
Guest - n/a Posts
#3: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hi;

Sorry - I forgot to mention - the API I give people is to pass in a
XPathNavigator. So it may not be an XmlDocument. And even if it is, I have no
way of accessing that object.

How do I do this for an XPathNavigator?

--
thanks - dave


"Kevin Yu [MSFT]" wrote:
[color=blue]
> Hi dave,
>
> You can set PreserveWhitespace to true, so that the space will be preserved.
>
> XmlDocument doc = new XmlDocument();
> doc.PreserveWhitespace = true;
> doc.LoadXml("<space> </space>");
> XmlNode n = doc.SelectSingleNode("space");
> MessageBox.Show(n.InnerXml.Length.ToString());
>
> HTH.
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>[/color]
Kevin Yu [MSFT]'s Avatar
Guest - n/a Posts
#4: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hi dave,

There is no way to preserve whitespace in the code directly with
XPathNavigator. You can try to do the following:

1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
</space>
2. If the node is a whitespace, check the XPathNavigator.NodeType property,
it has to be XPathNodeType.SignificantWhitespace or
XPathNodeType.Whitespace.

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

David Thielen's Avatar
Guest - n/a Posts
#5: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hi;

It's not my xml. I have a library that other people call and pass me their
xml. I wrote all of my code using XPathDocument & XPathNavigator because it
is the "suggested" approach.

But I need to have whitespace preserved. There must be a way as Microsoft
suggests using this approach and not using XmlDocument at all. Could you
please ask the .net team? I find it hard to believe they would not have made
it possible to do this.

--
thanks - dave


"Kevin Yu [MSFT]" wrote:
[color=blue]
> Hi dave,
>
> There is no way to preserve whitespace in the code directly with
> XPathNavigator. You can try to do the following:
>
> 1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
> </space>
> 2. If the node is a whitespace, check the XPathNavigator.NodeType property,
> it has to be XPathNodeType.SignificantWhitespace or
> XPathNodeType.Whitespace.
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>[/color]
Chris Lovett's Avatar
Guest - n/a Posts
#6: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

I'm on the .net xml team and I know for sure that once you've loaded an
XmlDocument without preserveWhitespace, you have lost all insignificant
whitespace information and there is no way for the XPathNavigator to
"reinvent" it on the fly. Therefore you'll have to tell folks who are
calling you not to do that. Or you chould change your component design so
that they call you to load the XML that way you can be in control of how the
whitespace processing happens.

"David Thielen" <thielen@nospam.nospam> wrote in message
news:71AED216-F755-4726-B5C1-6EDDFC5B332A@microsoft.com...[color=blue]
> Hi;
>
> It's not my xml. I have a library that other people call and pass me their
> xml. I wrote all of my code using XPathDocument & XPathNavigator because
> it
> is the "suggested" approach.
>
> But I need to have whitespace preserved. There must be a way as Microsoft
> suggests using this approach and not using XmlDocument at all. Could you
> please ask the .net team? I find it hard to believe they would not have
> made
> it possible to do this.
>
> --
> thanks - dave
>
>
> "Kevin Yu [MSFT]" wrote:
>[color=green]
>> Hi dave,
>>
>> There is no way to preserve whitespace in the code directly with
>> XPathNavigator. You can try to do the following:
>>
>> 1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
>> </space>
>> 2. If the node is a whitespace, check the XPathNavigator.NodeType
>> property,
>> it has to be XPathNodeType.SignificantWhitespace or
>> XPathNodeType.Whitespace.
>>
>> Kevin Yu
>> =======
>> "This posting is provided "AS IS" with no warranties, and confers no
>> rights."
>>
>>[/color][/color]


David Thielen's Avatar
Guest - n/a Posts
#7: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hello;

First off, thank you for answering. Getting a definite no helps as then I
know that I'm not missing something.

Most cases I am passed a Stream so I can call new XmlDocument(Stream)
instead of new XPathDocument(Stream). But all of the documentation I have
read says that XPathDocument is a lot faster for heavy xpath use - so do I
then lose that efficiency?

And out of curiosity, why does XPathDocument eat all of the whitespace?
Because xml schemes like Excel's SpreadsheetML need it.

--
thanks - dave


"Chris Lovett" wrote:
[color=blue]
> I'm on the .net xml team and I know for sure that once you've loaded an
> XmlDocument without preserveWhitespace, you have lost all insignificant
> whitespace information and there is no way for the XPathNavigator to
> "reinvent" it on the fly. Therefore you'll have to tell folks who are
> calling you not to do that. Or you chould change your component design so
> that they call you to load the XML that way you can be in control of how the
> whitespace processing happens.
>
> "David Thielen" <thielen@nospam.nospam> wrote in message
> news:71AED216-F755-4726-B5C1-6EDDFC5B332A@microsoft.com...[color=green]
> > Hi;
> >
> > It's not my xml. I have a library that other people call and pass me their
> > xml. I wrote all of my code using XPathDocument & XPathNavigator because
> > it
> > is the "suggested" approach.
> >
> > But I need to have whitespace preserved. There must be a way as Microsoft
> > suggests using this approach and not using XmlDocument at all. Could you
> > please ask the .net team? I find it hard to believe they would not have
> > made
> > it possible to do this.
> >
> > --
> > thanks - dave
> >
> >
> > "Kevin Yu [MSFT]" wrote:
> >[color=darkred]
> >> Hi dave,
> >>
> >> There is no way to preserve whitespace in the code directly with
> >> XPathNavigator. You can try to do the following:
> >>
> >> 1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
> >> </space>
> >> 2. If the node is a whitespace, check the XPathNavigator.NodeType
> >> property,
> >> it has to be XPathNodeType.SignificantWhitespace or
> >> XPathNodeType.Whitespace.
> >>
> >> Kevin Yu
> >> =======
> >> "This posting is provided "AS IS" with no warranties, and confers no
> >> rights."
> >>
> >>[/color][/color]
>
>
>[/color]
Chris Lovett's Avatar
Guest - n/a Posts
#8: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

XPathDocument (and XmlDocument) default to preserveWhitespace=false for
backward compatibility reasons.

But you can tell both of these to preservice whitespace as follows:

new XPathDocument(@"..\\..\\xmlfile1.xml", XmlSpace.Preserve);

and
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;


"David Thielen" <thielen@nospam.nospam> wrote in message
news:8FFC95E9-B71E-477B-A428-6B17601A3CDD@microsoft.com...[color=blue]
> Hello;
>
> First off, thank you for answering. Getting a definite no helps as then I
> know that I'm not missing something.
>
> Most cases I am passed a Stream so I can call new XmlDocument(Stream)
> instead of new XPathDocument(Stream). But all of the documentation I have
> read says that XPathDocument is a lot faster for heavy xpath use - so do I
> then lose that efficiency?
>
> And out of curiosity, why does XPathDocument eat all of the whitespace?
> Because xml schemes like Excel's SpreadsheetML need it.
>
> --
> thanks - dave
>
>
> "Chris Lovett" wrote:
>[color=green]
>> I'm on the .net xml team and I know for sure that once you've loaded an
>> XmlDocument without preserveWhitespace, you have lost all insignificant
>> whitespace information and there is no way for the XPathNavigator to
>> "reinvent" it on the fly. Therefore you'll have to tell folks who are
>> calling you not to do that. Or you chould change your component design
>> so
>> that they call you to load the XML that way you can be in control of how
>> the
>> whitespace processing happens.
>>
>> "David Thielen" <thielen@nospam.nospam> wrote in message
>> news:71AED216-F755-4726-B5C1-6EDDFC5B332A@microsoft.com...[color=darkred]
>> > Hi;
>> >
>> > It's not my xml. I have a library that other people call and pass me
>> > their
>> > xml. I wrote all of my code using XPathDocument & XPathNavigator
>> > because
>> > it
>> > is the "suggested" approach.
>> >
>> > But I need to have whitespace preserved. There must be a way as
>> > Microsoft
>> > suggests using this approach and not using XmlDocument at all. Could
>> > you
>> > please ask the .net team? I find it hard to believe they would not have
>> > made
>> > it possible to do this.
>> >
>> > --
>> > thanks - dave
>> >
>> >
>> > "Kevin Yu [MSFT]" wrote:
>> >
>> >> Hi dave,
>> >>
>> >> There is no way to preserve whitespace in the code directly with
>> >> XPathNavigator. You can try to do the following:
>> >>
>> >> 1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
>> >> </space>
>> >> 2. If the node is a whitespace, check the XPathNavigator.NodeType
>> >> property,
>> >> it has to be XPathNodeType.SignificantWhitespace or
>> >> XPathNodeType.Whitespace.
>> >>
>> >> Kevin Yu
>> >> =======
>> >> "This posting is provided "AS IS" with no warranties, and confers no
>> >> rights."
>> >>
>> >>[/color]
>>
>>
>>[/color][/color]


David Thielen's Avatar
Guest - n/a Posts
#9: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Yes - thank you.

I didn't see it for XPathDocument because the Stream ctor doesn't have that
option. But I can use the Stream to create an XmlDocument, Load it with the
Stream, then pass an XmlNodeReader.

--
thanks - dave


"Chris Lovett" wrote:
[color=blue]
> XPathDocument (and XmlDocument) default to preserveWhitespace=false for
> backward compatibility reasons.
>
> But you can tell both of these to preservice whitespace as follows:
>
> new XPathDocument(@"..\\..\\xmlfile1.xml", XmlSpace.Preserve);
>
> and
> XmlDocument xdoc = new XmlDocument();
> xdoc.PreserveWhitespace = true;
>
>
> "David Thielen" <thielen@nospam.nospam> wrote in message
> news:8FFC95E9-B71E-477B-A428-6B17601A3CDD@microsoft.com...[color=green]
> > Hello;
> >
> > First off, thank you for answering. Getting a definite no helps as then I
> > know that I'm not missing something.
> >
> > Most cases I am passed a Stream so I can call new XmlDocument(Stream)
> > instead of new XPathDocument(Stream). But all of the documentation I have
> > read says that XPathDocument is a lot faster for heavy xpath use - so do I
> > then lose that efficiency?
> >
> > And out of curiosity, why does XPathDocument eat all of the whitespace?
> > Because xml schemes like Excel's SpreadsheetML need it.
> >
> > --
> > thanks - dave
> >
> >
> > "Chris Lovett" wrote:
> >[color=darkred]
> >> I'm on the .net xml team and I know for sure that once you've loaded an
> >> XmlDocument without preserveWhitespace, you have lost all insignificant
> >> whitespace information and there is no way for the XPathNavigator to
> >> "reinvent" it on the fly. Therefore you'll have to tell folks who are
> >> calling you not to do that. Or you chould change your component design
> >> so
> >> that they call you to load the XML that way you can be in control of how
> >> the
> >> whitespace processing happens.
> >>
> >> "David Thielen" <thielen@nospam.nospam> wrote in message
> >> news:71AED216-F755-4726-B5C1-6EDDFC5B332A@microsoft.com...
> >> > Hi;
> >> >
> >> > It's not my xml. I have a library that other people call and pass me
> >> > their
> >> > xml. I wrote all of my code using XPathDocument & XPathNavigator
> >> > because
> >> > it
> >> > is the "suggested" approach.
> >> >
> >> > But I need to have whitespace preserved. There must be a way as
> >> > Microsoft
> >> > suggests using this approach and not using XmlDocument at all. Could
> >> > you
> >> > please ask the .net team? I find it hard to believe they would not have
> >> > made
> >> > it possible to do this.
> >> >
> >> > --
> >> > thanks - dave
> >> >
> >> >
> >> > "Kevin Yu [MSFT]" wrote:
> >> >
> >> >> Hi dave,
> >> >>
> >> >> There is no way to preserve whitespace in the code directly with
> >> >> XPathNavigator. You can try to do the following:
> >> >>
> >> >> 1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
> >> >> </space>
> >> >> 2. If the node is a whitespace, check the XPathNavigator.NodeType
> >> >> property,
> >> >> it has to be XPathNodeType.SignificantWhitespace or
> >> >> XPathNodeType.Whitespace.
> >> >>
> >> >> Kevin Yu
> >> >> =======
> >> >> "This posting is provided "AS IS" with no warranties, and confers no
> >> >> rights."
> >> >>
> >> >>
> >>
> >>
> >>[/color][/color]
>
>
>[/color]
=?Utf-8?B?Q2hyaXN0aWFuIEw=?='s Avatar
=?Utf-8?B?Q2hyaXN0aWFuIEw=?= June 27th, 2008 08:20 PM
Guest - n/a Posts
#10: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Hi there,

I have the same issue, but even if I use XmlDocument and set the
..PreserveWhitespace to true, whe I deserialize the xml, this node comes with
null value.

Christian Llanos


"David Thielen" wrote:
Quote:
Originally Posted by
Yes - thank you.
>
I didn't see it for XPathDocument because the Stream ctor doesn't have that
option. But I can use the Stream to create an XmlDocument, Load it with the
Stream, then pass an XmlNodeReader.
>
--
thanks - dave
>
>
"Chris Lovett" wrote:
>
Quote:
Originally Posted by
XPathDocument (and XmlDocument) default to preserveWhitespace=false for
backward compatibility reasons.

But you can tell both of these to preservice whitespace as follows:

new XPathDocument(@"..\\..\\xmlfile1.xml", XmlSpace.Preserve);

and
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;


"David Thielen" <thielen@nospam.nospamwrote in message
news:8FFC95E9-B71E-477B-A428-6B17601A3CDD@microsoft.com...
Quote:
Originally Posted by
Hello;
>
First off, thank you for answering. Getting a definite no helps as then I
know that I'm not missing something.
>
Most cases I am passed a Stream so I can call new XmlDocument(Stream)
instead of new XPathDocument(Stream). But all of the documentation I have
read says that XPathDocument is a lot faster for heavy xpath use - so do I
then lose that efficiency?
>
And out of curiosity, why does XPathDocument eat all of the whitespace?
Because xml schemes like Excel's SpreadsheetML need it.
>
--
thanks - dave
>
>
"Chris Lovett" wrote:
>
>I'm on the .net xml team and I know for sure that once you've loaded an
>XmlDocument without preserveWhitespace, you have lost all insignificant
>whitespace information and there is no way for the XPathNavigator to
>"reinvent" it on the fly. Therefore you'll have to tell folks who are
>calling you not to do that. Or you chould change your component design
>so
>that they call you to load the XML that way you can be in control of how
>the
>whitespace processing happens.
>>
>"David Thielen" <thielen@nospam.nospamwrote in message
>news:71AED216-F755-4726-B5C1-6EDDFC5B332A@microsoft.com...
Hi;
>
It's not my xml. I have a library that other people call and pass me
their
xml. I wrote all of my code using XPathDocument & XPathNavigator
because
it
is the "suggested" approach.
>
But I need to have whitespace preserved. There must be a way as
Microsoft
suggests using this approach and not using XmlDocument at all. Could
you
please ask the .net team? I find it hard to believe they would not have
made
it possible to do this.
>
--
thanks - dave
>
>
"Kevin Yu [MSFT]" wrote:
>
>Hi dave,
>>
>There is no way to preserve whitespace in the code directly with
>XPathNavigator. You can try to do the following:
>>
>1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
></space>
>2. If the node is a whitespace, check the XPathNavigator.NodeType
>property,
>it has to be XPathNodeType.SignificantWhitespace or
>XPathNodeType.Whitespace.
>>
>Kevin Yu
>=======
>"This posting is provided "AS IS" with no warranties, and confers no
>rights."
>>
>>
>>
>>
>>




Martin Honnen's Avatar
Martin Honnen June 27th, 2008 08:20 PM
Guest - n/a Posts
#11: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Christian L wrote:
Quote:
Originally Posted by
I have the same issue, but even if I use XmlDocument and set the
.PreserveWhitespace to true, whe I deserialize the xml, this node comes with
null value.


What issue exactly do you have? You are loading an XML document into an
XmlDocument object where PreserveWhitespace is set to true and then you
do what exactly where "this node comes up with null value"? In the DOM
object model the Value property of an XmlElement is always null. Is that
the problem? Use InnerText instead.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Christian Llanos's Avatar
Christian Llanos June 27th, 2008 08:20 PM
Guest - n/a Posts
#12: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

On Apr 25, 1:29*pm, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Originally Posted by
Christian L wrote:
Quote:
Originally Posted by
I have the same issue, but even if I use XmlDocument and set the
.PreserveWhitespace to true, whe I deserialize the xml, this node comes with
null value.

>
What issue exactly do you have? You are loading an XML document into an
XmlDocument object where PreserveWhitespace is set to true and then you
do what exactly where "this node comes up with null value"? In the DOM
object model the Value property of an XmlElement is always null. Is that
the problem? Use InnerText instead.
>
--
>
* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/


What I try to do is deserialize the xml document, but when I do that,
the xml elemet with the innertext with all whitespaces comes with a
null value instead of the whitespaces.

Christian Llanos
Martin Honnen's Avatar
Martin Honnen June 27th, 2008 08:20 PM
Guest - n/a Posts
#13: Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s

Christian Llanos wrote:
Quote:
Originally Posted by
What I try to do is deserialize the xml document, but when I do that,
the xml elemet with the innertext with all whitespaces comes with a
null value instead of the whitespaces.


Please post your code, both the XML as well as the code you use for
deserializing.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 
Not the answer you were looking for? Post your question . . .
196,824 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,824 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors