Connecting Tech Pros Worldwide Forums | Help | Site Map

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

David Thielen
Guest
 
Posts: n/a
#1: Nov 12 '05
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

Kevin Yu [MSFT]
Guest
 
Posts: n/a
#2: Nov 12 '05

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
Guest
 
Posts: n/a
#3: Nov 12 '05

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]
Guest
 
Posts: n/a
#4: Nov 12 '05

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
Guest
 
Posts: n/a
#5: Nov 12 '05

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
Guest
 
Posts: n/a
#6: Nov 12 '05

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
Guest
 
Posts: n/a
#7: Nov 12 '05

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
Guest
 
Posts: n/a
#8: Nov 12 '05

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
Guest
 
Posts: n/a
#9: Nov 12 '05

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=?=
Guest
 
Posts: n/a
#10: Jun 27 '08

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:
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:
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:
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
Guest
 
Posts: n/a
#11: Jun 27 '08

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


Christian L wrote:
Quote:
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
Guest
 
Posts: n/a
#12: Jun 27 '08

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


On Apr 25, 1:29*pm, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Christian L wrote:
Quote:
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
Guest
 
Posts: n/a
#13: Jun 27 '08

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


Christian Llanos wrote:
Quote:
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/
Closed Thread