> I have an XML file that I am reading in VS 2005.
>> I have an XML file that I am reading in VS 2005.
>
[quoted text clipped - 12 lines]
>
> Try with .FirstChild.Value instead og .InnerText !
I assume that you are saying that the innerText is actually a child of the
node?
I changed the code to show:
foreach (XmlNode childNode in itemNode.ChildNodes)
{
sw.WriteLine("{0}Child Node: {1} Value: {2} InnerText: {3}\n",
spaces,
childNode.Name,
childNode.FirstChild.Value,
childNode.InnerText);
// Does this Child Node have any Child Nodes
This gives me an error saying that:
Object reference not set to an instance of an object.
But as soon as I changed it to check if there were any child nodes, then it
worked fine.
if (childNode.ChildNodes.Count > 0)
sw.WriteLine("{0}Child Node: {1} Value: {2} InnerText: {3}\n",
spaces,
childNode.Name,
childNode.FirstChild.Value,
childNode.InnerText);
else
sw.WriteLine("{0}Child Node: {1} Value: {2} InnerText: {3}\n",
spaces,
childNode.Name,
"",
childNode.InnerText);
Also, I found that I needed to access the attributes of a parent 2 levels
away. Is that possible?
For example, using the same xml page:
<?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>
If my childnode is now at "OTHERFILENUMBER", childnode.Parent = FIELDS. but
I need to get to "FORM" as that has all the attributes I need.
Is there a way to directly access that node from the childNode?
Thanks,
Tom
Arne Vajhøj - 28 Mar 2008 19:36 GMT
>>> I have an XML file that I am reading in VS 2005.
>>> I am trying to get the innerText of each of the fields below <FIELDS> so
[quoted text clipped - 13 lines]
> I assume that you are saying that the innerText is actually a child of the
> node?
No I am saying that .InnerText get all text from the element and
the sublements while .FirstChild.Value will just get you the text
within this element.
> This gives me an error saying that:
>
> Object reference not set to an instance of an object.
>
> But as soon as I changed it to check if there were any child nodes, then it
> worked fine.
It is not surprising that .FirstChild returns null if there are
no child nodes.
> If my childnode is now at "OTHERFILENUMBER", childnode.Parent = FIELDS. but
> I need to get to "FORM" as that has all the attributes I need.
>
> Is there a way to directly access that node from the childNode?
You can use .Parent multiple times !
Arne
tshad - 29 Mar 2008 00:01 GMT
>>>> I have an XML file that I am reading in VS 2005.
>>>> I am trying to get the innerText of each of the fields below <FIELDS>
[quoted text clipped - 37 lines]
>
> You can use .Parent multiple times !
So I can do childNode.Parent.Parent.Parent.Name or
childNode.Parent.Parent.Parent.FirstChild.Value.
That would solve my problem.
Thanks,
Tom
> Arne
Arne Vajhøj - 29 Mar 2008 01:08 GMT
> So I can do childNode.Parent.Parent.Parent.Name or
> childNode.Parent.Parent.Parent.FirstChild.Value.
Yes.
(I am just thinking - is it .Parent or .ParentNode - never
mind - the friendly compiler will tell you)
Arne