T
tshad
I have an XML file that I am reading in VS 2005. The code is something
like:
XmlDocument itemDoc = new XmlDocument();
itemDoc.Load(@"c:\TestDocs\1004win.xml");
// iterate through top-level elements
foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
{
if (itemNode.Attributes.Count > 0)
{
spaces += " ";
foreach (XmlAttribute xmlAttribute in itemNode.Attributes)
{
sw.WriteLine("\n{0}Attribute Name: {1} Value: {2}\n", spaces,
xmlAttribute.Name, xmlAttribute.Value);
}
spaces.Remove(0, 4);
}
if (itemNode.ChildNodes.Count == 0)
sw.WriteLine("(No additional Information)\n");
else
{
if (itemNode.ChildNodes.Count > 0)
{
spaces += " ";
foreach (XmlNode childNode in itemNode.ChildNodes)
{
sw.WriteLine("{0}Child Node: {1} Value: {2} InnerText: {3}\n",
spaces,
childNode.Name,
childNode.Value,
childNode.InnerText);
}
spaces.Remove(0, 4);
}
}
}
My XML document looks like:
<?xml version="1.0" encoding="utf-8"?>
<REPORT VERSION="1.10" FILENUM="" DESCRIPTION="Form Utility XML: 3/18/2008
12:27:13 PM" MAJORFORM="100">
<ORDER></ORDER>
<TRACKING></TRACKING>
<FORMS>
<FORM NUM="1" FORMCODE="1004" SECCODE="1" >
<FIELDS>
<OTHERFILENUMBER>692</OTHERFILENUMBER>
<FNMA_FILENUMBER>693</FNMA_FILENUMBER>
<SUBPROPADDRESS>3</SUBPROPADDRESS>
</FIELDS>
<FORMPHOTOS></FORMPHOTOS>
<ATTACHMENTS></ATTACHMENTS>
</FORM>
</FORMS>
</REPORT>
I am trying to get the innerText of each of the fields below <FIELDS> so I
use childNode.InnerText to get it.
But I also found that if there are child nodes under your nodes the
innerText will include that as well.
In my example, the innerText for <Form> is 6926933 - which is the innerText
of <OTHERFILENUMBER>, <FNMA_FILENUMBER> and <SUBPROPADDRESS>???
Why is that and how can I get just the innerText of a particular element?
I would have assumed that the innerText of Form would have been "".
Thanks,
Tom
like:
XmlDocument itemDoc = new XmlDocument();
itemDoc.Load(@"c:\TestDocs\1004win.xml");
// iterate through top-level elements
foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
{
if (itemNode.Attributes.Count > 0)
{
spaces += " ";
foreach (XmlAttribute xmlAttribute in itemNode.Attributes)
{
sw.WriteLine("\n{0}Attribute Name: {1} Value: {2}\n", spaces,
xmlAttribute.Name, xmlAttribute.Value);
}
spaces.Remove(0, 4);
}
if (itemNode.ChildNodes.Count == 0)
sw.WriteLine("(No additional Information)\n");
else
{
if (itemNode.ChildNodes.Count > 0)
{
spaces += " ";
foreach (XmlNode childNode in itemNode.ChildNodes)
{
sw.WriteLine("{0}Child Node: {1} Value: {2} InnerText: {3}\n",
spaces,
childNode.Name,
childNode.Value,
childNode.InnerText);
}
spaces.Remove(0, 4);
}
}
}
My XML document looks like:
<?xml version="1.0" encoding="utf-8"?>
<REPORT VERSION="1.10" FILENUM="" DESCRIPTION="Form Utility XML: 3/18/2008
12:27:13 PM" MAJORFORM="100">
<ORDER></ORDER>
<TRACKING></TRACKING>
<FORMS>
<FORM NUM="1" FORMCODE="1004" SECCODE="1" >
<FIELDS>
<OTHERFILENUMBER>692</OTHERFILENUMBER>
<FNMA_FILENUMBER>693</FNMA_FILENUMBER>
<SUBPROPADDRESS>3</SUBPROPADDRESS>
</FIELDS>
<FORMPHOTOS></FORMPHOTOS>
<ATTACHMENTS></ATTACHMENTS>
</FORM>
</FORMS>
</REPORT>
I am trying to get the innerText of each of the fields below <FIELDS> so I
use childNode.InnerText to get it.
But I also found that if there are child nodes under your nodes the
innerText will include that as well.
In my example, the innerText for <Form> is 6926933 - which is the innerText
of <OTHERFILENUMBER>, <FNMA_FILENUMBER> and <SUBPROPADDRESS>???
Why is that and how can I get just the innerText of a particular element?
I would have assumed that the innerText of Form would have been "".
Thanks,
Tom