Re: Editting an XML file



Hi,

Thanks for these.

I did try GetElementByTagName but gave up on that, for exactly the problem
you outlined, as I have nested elements with the same name. (I am not
controlling the name). THe method you have outlined here is similar to what
I have written already (though I have done the loop inside the function
rather than calling a seperate function to do the loop).

I have another problem now and not sure what is causing it...

A whole segment of my XML is disappearing during my save. This is really odd
as I am not reading or writing any of this section.

Basically, I am loading the XML at start-up and putting it into an
XMLDocument that is globally accessed.
I am parsing the XML similar to what you have outlined below and building up
a datatable to be used in my app.
I then display the first record.
Even without doing anything to the XML itself, when I click my save button,
I am just saving the data in exactly the opposite way that I collect the
data from the XML file, yet a good chunk is disappearing that I am not even
touching. (How confused am I?)

Based on my previous tree, there are nodes and leafs between PartImage and
PartRegions (they are earlier siblings to PartRegions (the first level
PartRegions)). This area is absolutely critical to the functionality of the
desktop version of the app and I really don't understand why it is
disappearing.

I will try and do a debug, but I don't know where the problem will lie. Even
though you haven't seen the code yet, do you know of anything that I might
have overlooked that would cause this?

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Martin Honnen" <mahotrash@xxxxxxxx> wrote in message
news:uaZKeYTWIHA.3556@xxxxxxxxxxxxxxxxxxxxxxx
Martin Honnen wrote:

If you have elements of the same name at different levels then
GetElementsByTagName is kind of dangerous however as it gives the
elements at all levels and it flattens the hierarchy and the nesting. In
that case you might need loop through ChildNodes of a node and check the
NodeType == XmlNodeType.Element to find child elements, if needed you can
add a check for the Name.

Here is an example how you could build your own method to find child
elements of a certain name:

static ArrayList GetChildElements (XmlNode node, string name)
{
ArrayList elements = new ArrayList();
foreach (XmlNode child in node.ChildNodes)
{
if (child.NodeType == XmlNodeType.Element && child.Name == name)
{
elements.Add(child);
}
}
return elements;
}

used like this:

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<root>
<foo>
<child>
<child>
<bar></bar>
</child>
<item>value 1</item>
</child>
<whatever/>
<child>
<item>value 2</item>
</child>
</foo>
</root>");
ArrayList childElements = GetChildElements(doc.DocumentElement["foo"],
"child");
foreach (XmlNode node in childElements)
{
Console.WriteLine(node["item"].InnerText);
}


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


.