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.
VulpesPosted Mar 3, 2011, 4:56 PM
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.
Suthish NairPosted Mar 7, 2011, 2:14 PM
Mark MaguirePosted Mar 4, 2011, 5:23 AM
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 NairPosted Mar 3, 2011, 1:53 PM
VulpesPosted Mar 3, 2011, 10:53 AM
However, as far as I can see, that's the only way to get any sort of reader out of XDocument.
Mark MaguirePosted Mar 3, 2011, 10:32 AM
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
VulpesPosted Mar 3, 2011, 10:19 AM
oXDocument = XDocument.Load(oXMLTextReader);
XmlTextReader oReader = (XmlTextReader)oXDocument.CreateReader();
while (oReader.Read())
{
// etc
}