XDocument oXDocument = XDocument.Load(m_sFile);
I want to know if I pass the Load method of the XDocument an XmlTextReader object in the manner shown below, can I interact with the loading mechanism using the XmlTextReader as if I were to use the Read method of XmlTextReader in a conventional manner?
Conventional XmlTextReader use
-------------------------------
while (oReader.Read())
{
switch (oReader.NodeType)
{
case XmlNodeType.Element:
//DO SOMETHING
break;
}
}
Passing the XmlTextReader
-------------------------
using (XmlTextReader oXMLTextReader = new XmlTextReader(m_sFile))
{
oXMLTextReader.MoveToContent();
oXDocument = XDocument.Load(oXMLTextReader);
}
Any help is most welcome.

VulpesRank #55 · 29.2K reputationPosted Mar 3, 2011, 11:56 AM EST
Suppose, for example, we have this persons.xml file:
New York
London
Paris
Then the following code, if I've written it correctly, should create an XDocument using just the first Person node (ignoring the other two) and change the content of the Address node from New York to Washington:
using System;
using System.Xml;
using System.Xml.Linq;
class Program
{
static void Main()
{
string m_sFile = "persons.xml";
var persons = new XElement("Persons");
XElement person = null;
var settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (var oReader = XmlTextReader.Create(m_sFile, settings))
{
oReader.ReadStartElement("Persons");
int count = 1;
while (oReader.Name == "Person")
{
person = (XElement)XNode.ReadFrom(oReader);
person.Element("Address").Value = "Washington";
if (count == 1) persons.Add(person);
count++;
}
oReader.ReadEndElement();
}
var doc = new XDocument();
doc.Add(persons);
Console.WriteLine(doc);
Console.ReadKey();
}
}
In practice, of course, you could do any manipulations you like as the file is read.
VulpesRank #55 · 29.2K reputationPosted Mar 3, 2011, 5:19 AM EST
oXDocument = XDocument.Load(oXMLTextReader);
XmlTextReader oReader = (XmlTextReader)oXDocument.CreateReader();
while (oReader.Read())
{
// etc
}
Mark Maguire17 reputationPosted Mar 3, 2011, 5:32 AM EST
This does not work for me as the data is loaded first before reaching the line stating:
XmlTextReader oReader = (XmlTextReader)oXDocument.CreateReader();
This line also fails to cast an XNodeReader to an XmlTextReader.
I need to be able to interact with the load method as it executes in some way, either that or dynamically populate the XDocument using the XmlTextReader without using the XDocument.Load method.
Any more ideas?
Cheers
VulpesRank #55 · 29.2K reputationPosted Mar 3, 2011, 5:53 AM EST
However, as far as I can see, that's the only way to get any sort of reader out of XDocument.
Suthish NairRank #92 · 22.8K reputationPosted Mar 3, 2011, 8:53 AM EST
Mark Maguire17 reputationPosted Mar 4, 2011, 12:23 AM EST
I've managed to get my file to dynamically load in.
I ended up implementing the ReadSubTree method which creates an XMLReader, this allowed me to recursively iterate all the child nodes (regardless of how deep they are) and manipulate the data as it loads. It also now allows me to implement progress information.
Thanks for your help!
Suthish NairRank #92 · 22.8K reputationPosted Mar 7, 2011, 9:14 AM EST