Serialize XPathNavigator

B

Bruce Sandeman

Hi,
Does anyone know how to serialize an XPathNavigator object?
I have tried the following but it moans that the xpn does not have a parameterless
constructor.

XPathNavigator xpn = e.Source.SelectSingleNode("/ns1:Invoice/ns1:InvoiceDetail/ns1:Lines/ns1:Line/ns1:Matches/ns1:Match/ns1:SubLedger",
this.NamespaceManager);

XmlSerializer x = new XmlSerializer(xpn.GetType());
StringWriter tw = new StringWriter();

x.Serialize(tw, xpn);
StringBuilder sbldr = tw.GetStringBuilder();

_tmpXML = sbldr.ToString();

thanks very much
cheers
Bruce
 
M

Marc Gravell

What are you actually trying to do? Persist the underlying xml? In which
case, .WriteSubtree(xmlWriter) may help.

If not, can you clarify your intent?

Marc
 
D

Dave Sexton

Hi Bruce,

You won't be able to serialize XPathNavigator directly because it's not marked as Serializable.

Why not just serialize the outer-xml of the particular node that you have selected in your example as a string?

If there are particular settings in the XPathNavigator that you want to retain you could create a class that encapsulates those
settings and mark it as [Serializable].
 
B

Bruce Sandeman

Hi Dave,
Thanks, I will give that a go.

In response to Marc: I need the actual object to be serialized as it is a
reference to a particular element within a repeating group and I have no
way of knowing which instance I am in. (this is using InfoPath 2007).

cheers
Bruce

Hi Bruce,

You won't be able to serialize XPathNavigator directly because it's
not marked as Serializable.

Why not just serialize the outer-xml of the particular node that you
have selected in your example as a string?

If there are particular settings in the XPathNavigator that you want
to retain you could create a class that encapsulates those settings
and mark it as [Serializable].

Hi,
Does anyone know how to serialize an XPathNavigator object?
I have tried the following but it moans that the xpn does not have a
parameterless constructor.
XPathNavigator xpn =

e.Source.SelectSingleNode("/ns1:Invoice/ns1:InvoiceDetail/ns1:Lines/n
s1:Line/ns1:Matches/ns1:Match/ns1:SubLedger",

this.NamespaceManager);

XmlSerializer x = new XmlSerializer(xpn.GetType());

StringWriter tw = new StringWriter();

x.Serialize(tw, xpn);
StringBuilder sbldr = tw.GetStringBuilder();
_tmpXML = sbldr.ToString();

thanks very much
cheers
Bruce
 
Top