XML insert element to a certain nested child

K

Kim

Im trying to add an element (<Tag>value</Tag>) under a certain child,
but something goes wrong. It gets inserted just before the root end tag
instead of the location I tought it would.
I load an exsisting xml file, so I have the structure. I simply want to
update it.

What am I doing wrong? better ways of inserting elements are
appreciatived.


C# code:
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(filename);

System.Xml.XmlNodeList appItems;
System.Xml.XmlNode appItems_child;
System.Xml.XmlElement xElement;

appItems = xDoc.GetElementsByTagName("Folder"); //used in a loop to
determine if an new element should be added

appItems_child = xDoc.DocumentElement;

xElement = xDoc.CreateElement("Folder");
xElement.InnerText = "some string path";
appItems_child.InsertAfter(xElement, appItems_child.LastChild);

XML file input (cut out):
<Configuration>
<Options>
<Settings>
<SourceFolders>
<Folder>C:\</Folder>
<Folder>D:\Test\</Folder>
</SourceFolders>
</Settings>
</Options>
</Configuration>

XML file output (cut out):
<Configuration>
<Options>
<Settings>
<SourceFolders>
<Folder>C:\</Folder>
<Folder>D:\Test\</Folder>
</SourceFolders>
</Settings>
</Options>
<Folder>new path string</Folder> // this is wrong. should have been
under <SourceFolders>
</Configuration>
 
M

Marc Gravell

That's because appItems_child is the document element (Configuration), so
it's last child is Options (since it only has the one child); it is doing
what you asked: adding the new node after Options...

I suggest you use SelectSingleNode to get hold of the SourceFolders element,
and then call .AppendChild(); this will add your new node as the last child
of SourceFolders, which is what you want

The exact XPath/XQuery will depend on the "real" xml, but something like
"/Configuration/Options/Settings/SourceFolders" would do - or even just
"//SourceFolders" (although less efficient)...

Marc
 
K

Kim

Having changed the code (see below) to such as you suggested, I get an
error. "Object reference not set to an instance of an object." needness
to say that it crashes. Any ideas why this error happends ?

However if I use the previous code, but changing the appItems_child
line to "appItems_child = appItems[0].ParentNode;" that works fine. I
do know this will give an error if index 0 doesnt exsist (in which case
no <Folder> tag is present under <SourceFolders>).

C# code:
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(filename);

System.Xml.XmlNodeList appItems;
System.Xml.XmlNode appItems_child;
System.Xml.XmlElement xElement;

appItems = xDoc.GetElementsByTagName("Folder"); //used in a loop to
determine if an new element should be added

appItems_child = xDoc.SelectSingleNode("SourceFolders");

xElement = xDoc.CreateElement("Folder");
xElement.InnerText = "some string path";
appItems_child.AppendChild(xElement);
 
M

Marc Gravell

..SelectSingleNode("SourceFolders") means "return the first child element of
the current node called SourceFolders (if any, else return null).

xDoc does not /have/ a child called SourceFolders. Either use
GetElementsByTagName, or give it the proper XPath/XQuery expression. Hint: I
already told you it (I think it's right - maybe a bugette in there; I
haven't tested it).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top