I have an XML file with no root. I cannot change this. I am trying to parse it, but XDocument.Load
won't do it. I have tried to set ConformanceLevel.Fragment
, but I still get an exception thrown. Does anyone have a solution to this?
I tried with XmlReader
, but things are messed up and can't get it work right. XDocument.Load
works great, but if I have a file with multiple roots, it doesn't.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
XmlReader
itself does support reading of xml fragment - i.e.
var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment }; using (var reader = XmlReader.Create("fragment.xml", settings)) { // you can work with reader just fine }
However XDocument.Load
does not support reading of fragmented xml.
Quick and dirty way is to wrap the nodes under one virtual root before you invoke the XDocument.Parse
. Like:
var fragments = File.ReadAllText("fragment.xml"); var myRootedXml = "<root>" + fragments + "</root>"; var doc = XDocument.Parse(myRootedXml);
This approach is limited to small xml files - as you have to read file into memory first; and concatenating large string means moving large objects in memory - which is best avoided.
If performance matters you should be reading nodes into XDocument
one-by-one via XmlReader
as explained in excellent @Martin-Honnen 's answer (https://stackoverflow.com/a/18203952/2440262)
If you use API that takes for granted that XmlReader
iterates over valid xml, and performance matters, you can use joined-stream approach instead:
using (var jointStream = new MultiStream()) using (var openTagStream = new MemoryStream(Encoding.ASCII.GetBytes("<root>"), false)) using (var fileStream = File.Open(@"fragment.xml", FileMode.Open, FileAccess.Read, FileShare.Read)) using (var closeTagStream = new MemoryStream(Encoding.ASCII.GetBytes("</root>"), false)) { jointStream.AddStream(openTagStream); jointStream.AddStream(fileStream); jointStream.AddStream(closeTagStream); using (var reader = XmlReader.Create(jointStream)) { // now you can work with reader as if it is reading valid xml } }
MultiStream - see for example https://gist.github.com/svejdo1/b9165192d313ed0129a679c927379685
Note: XDocument
loads the whole xml into memory. So don't use it for large files - instead use XmlReader
for iteration and load just the crispy bits as XElement
via XNode.ReadFrom(...)
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