R
roger_27
I'm having quite a problem here, I need to take an XML file, read it and
write it out using XMLTextWriter to a differet file as it reads. After it's
done reading, it will add some stuff to the end.
I want to use xmltextreader/xmltextwriter because it's better with memory
than XMLDocument.
here's what I have, maybe someone out there will help me out:
//copy source to a temp spot.
File.Copy("old.txt", "tmp.txt", true);
XmlTextReader xds = new XmlTextReader("tmp.txt");
//actual XML File setup...
xmlwrite = new XmlTextWriter("new.txt", null);
//this will be indented text
xmlwrite.Formatting = System.Xml.Formatting.Indented;
//makes the xml version =1.0 a separate attribute...
//i dont this true or false really matters...
xmlwrite.WriteStartDocument(false);
while (xds.Read())
{
xds.MoveToElement();
if (xds.NodeType == XmlNodeType.Element)
{
xmlwrite.WriteElementString(xds.Name, xds.Value);
}
}
the Source XML looks like this:
<?xml version="1.0" standalone="no"?>
<Statements>
<Customer>
<NAME>William Jones</NAME>
<CUSNAME>William Jones</CUSNAME>
<CUSCO />
<CUSADR1>787 McFall</CUSADR1>
<CUSADR2 />
<CUSCSZ>Los Angeles, CA 95336-6941</CUSCSZ>
</Customer>
</Statements>
what's happening is it is reading the ELEMENT TEXT as an element.
for example, the first Read() after the xml declaration (and "<statements>",
and "<customer>") has
xds.Name = "NAME" and a value of blank.
xmlnodetype is Element
the next Read() reads
xds.Name = "" and a xds.Value = "William Jones"
xmlnodetype is Text
it seems to be reading the element as it's own thing, and the text as its
own thing.
can't I read the element and its value together?? any help is appreciated
here.
thanks.
write it out using XMLTextWriter to a differet file as it reads. After it's
done reading, it will add some stuff to the end.
I want to use xmltextreader/xmltextwriter because it's better with memory
than XMLDocument.
here's what I have, maybe someone out there will help me out:
//copy source to a temp spot.
File.Copy("old.txt", "tmp.txt", true);
XmlTextReader xds = new XmlTextReader("tmp.txt");
//actual XML File setup...
xmlwrite = new XmlTextWriter("new.txt", null);
//this will be indented text
xmlwrite.Formatting = System.Xml.Formatting.Indented;
//makes the xml version =1.0 a separate attribute...
//i dont this true or false really matters...
xmlwrite.WriteStartDocument(false);
while (xds.Read())
{
xds.MoveToElement();
if (xds.NodeType == XmlNodeType.Element)
{
xmlwrite.WriteElementString(xds.Name, xds.Value);
}
}
the Source XML looks like this:
<?xml version="1.0" standalone="no"?>
<Statements>
<Customer>
<NAME>William Jones</NAME>
<CUSNAME>William Jones</CUSNAME>
<CUSCO />
<CUSADR1>787 McFall</CUSADR1>
<CUSADR2 />
<CUSCSZ>Los Angeles, CA 95336-6941</CUSCSZ>
</Customer>
</Statements>
what's happening is it is reading the ELEMENT TEXT as an element.
for example, the first Read() after the xml declaration (and "<statements>",
and "<customer>") has
xds.Name = "NAME" and a value of blank.
xmlnodetype is Element
the next Read() reads
xds.Name = "" and a xds.Value = "William Jones"
xmlnodetype is Text
it seems to be reading the element as it's own thing, and the text as its
own thing.
can't I read the element and its value together?? any help is appreciated
here.
thanks.