string to XmlNode

C

Christof Nordiek

Given a string value representing an Xml-Element. What is the easyiest way
to get a XmlNode from it?
 
M

Martin Honnen

Christof said:
Given a string value representing an Xml-Element. What is the easyiest way
to get a XmlNode from it?

With .NET 1.x e.g.
XmlTextReader xmlReader = new XmlTextReader(new
StringReader(stringVariable));

// if you already have an XmlDocument then use that, otherwise
// create one
XmlDocument xmlDocument = new XmlDocument();
XmlNode node = xmlDocument.ReadNode(xmlReader);

The node can the be inserted/used as needed.

With .NET 2.0 you should use XmlReader.Create to create an XmlReader,
otherwise the code is the same. And XmlTextReader is still around in
..NET 2.0 so the code would even work without change, only using
XmlReader.Create is preferred in .NET 2.0.
 
C

Christof Nordiek

Thanks Martin

exactly what i needed.

Martin Honnen said:
With .NET 1.x e.g.
XmlTextReader xmlReader = new XmlTextReader(new
StringReader(stringVariable));

// if you already have an XmlDocument then use that, otherwise
// create one
XmlDocument xmlDocument = new XmlDocument();
XmlNode node = xmlDocument.ReadNode(xmlReader);

The node can the be inserted/used as needed.

With .NET 2.0 you should use XmlReader.Create to create an XmlReader,
otherwise the code is the same. And XmlTextReader is still around in .NET
2.0 so the code would even work without change, only using
XmlReader.Create is preferred in .NET 2.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top