If I have an xmlreader instance how can I use it to read its current node and end up with a xmlElement instance?
MoveToContent(); var data = reader. ReadElementContentAsString(); Console. WriteLine(data); In the example, we read the value from the simple XML document with XmlReader .
Creates a new XmlReader instance using the specified stream with default settings. Creates a new XmlReader instance by using the specified URI and settings.
XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument .
Not tested, but how about via an XmlDocument
:
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlElement el = doc.DocumentElement;
Alternatively (from the cmoment), something like:
doc.LoadXml(reader.ReadOuterXml());
But actually I'm not a fan of that... it forces an additional xml-parse step (one of the more CPU-expensive operations) for no good reason. If the original is being glitchy, then perhaps consider a sub-reader:
using (XmlReader subReader = reader.ReadSubtree())
{
XmlDocument doc = new XmlDocument();
doc.Load(subReader);
XmlElement el = doc.DocumentElement;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With