LINQ to XML - Load XML fragments from file
Here's how to do it with an XmlReader
, which is probably the most flexible and fastest-performing approach:
XmlReaderSettings xrs = new XmlReaderSettings();xrs.ConformanceLevel = ConformanceLevel.Fragment;XDocument doc = new XDocument(new XElement("root"));XElement root = doc.Descendants().First();using (StreamReader fs = new StreamReader("XmlFile1.xml"))using (XmlReader xr = XmlReader.Create(fs, xrs)){ while(xr.Read()) { if (xr.NodeType == XmlNodeType.Element) { root.Add(XElement.Load(xr.ReadSubtree())); } }}