XML Manipulation Going Haywire!

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

Sometimes it works, sometimes it doesn't!! Can someone pinpoint the bug or
alternative way..?

Tx

Input:
<root>
<element name="AC1">
<children name="chd1">
</children>
</element>
<element name="AC2">
<children name="chd2">
</element>
</root>

Output:
<root>
<element name="AC2">
<children name="chd2">
</element>
</root>

CS Code
===============
XmlDocument doc = new XmlDocument();
doc.Load(master);

XmlNode
deleteNode=doc.SelectSingleNode(String.Format("descendant::Item[@name='{0}']
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);
 
Sometimes it works, sometimes it doesn't!! Can someone pinpoint the bug or
alternative way..?

Tx

Input:
<root>
<element name="AC1">
<children name="chd1">
</children>
</element>
<element name="AC2">
<children name="chd2">
</element>
</root>

Output:
<root>
<element name="AC2">
<children name="chd2">
</element>
</root>

CS Code
===============
XmlDocument doc = new XmlDocument();
doc.Load(master);

XmlNode
deleteNode=doc.SelectSingleNode(String.Format("descendant::Item[@name='{0}'­]
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);

Seems the input is not well formatted:

second <children> node does not have a end tag. Are you trying to make
the input in a valid format using code?
 
forgive me for the sample xml [I just created] XML is well formed, I was
more trying see any bug in the code?


Sometimes it works, sometimes it doesn't!! Can someone pinpoint the bug or
alternative way..?

Tx

Input:
<root>
<element name="AC1">
<children name="chd1">
</children>
</element>
<element name="AC2">
<children name="chd2">
</element>
</root>

Output:
<root>
<element name="AC2">
<children name="chd2">
</element>
</root>

CS Code
===============
XmlDocument doc = new XmlDocument();
doc.Load(master);

XmlNode
deleteNode=doc.SelectSingleNode(String.Format("descendant::Item[@name='{0}'­
]
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);

Seems the input is not well formatted:

second <children> node does not have a end tag. Are you trying to make
the input in a valid format using code?
 
forgive me for the sample xml [I just created] XML is well formed, I was
more trying see any bug in the code?


Sometimes it works, sometimes it doesn't!! Can someone pinpoint the bugor
alternative way..?

Input:
<root>
<element name="AC1">
<children name="chd1">
</children>
</element>
<element name="AC2">
<children name="chd2">
</element>
</root>
Output:
<root>
<element name="AC2">
<children name="chd2">
</element>
</root>
CS Code
===============
XmlDocument doc = new XmlDocument();
doc.Load(master);

deleteNode=doc.SelectSingleNode(String.Format("descendant::Item[@name='{0}'­­
]
","AC1"));
doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);

Seems the input is not well formatted:

second <children> node does not have a end tag. Are you trying to make
the input in a valid format using code?- Hide quoted text -

- Show quoted text -

Not very sure about the descendant::Item expression. Used to use "/
root/element" kind of xpath.
Any problems you can find while debugging?
 
deleteNode =
doc.SelectSingleNode( String.Format( "descendant::Item[@name='{0}']",
"AC1"));

In general, you should be using more lines to write code like this, and you
should be using asserts:

string s = String.Format("descendant::Item[@name='{0}'], "AC1");
XmlNode n = doc.SelectSingleNode(s);
Debug.Assert(n != null);
Debug.Assert(n.Parent == doc.DocumentElement);
doc.DocumentElement.RemoveChild(n);

This will let you step through the code in the debugger and examine the
values of s and n, both of which are questionable. It also makes your code's
implicit assumptions explicit.

I see two obvious problems with this code:

The XPath you're formulating here looks like this:

descendant::Item[@name='AC1']

This will find the first descendant of the context element where the element
name is "Item" and whose "name" attribute is set to "AC1". Since none of the
elements in the instance document you've posted are named "Item", this won't
find anything. The first assert I added to your code would catch this.

If you want to find *any* descendant element whose "name" attribute is set
to "AC1", you'd use

descendant::*[@name='AC1']

Second: you're searching on the descendant axis, but you're deleting a
child. This will throw an exception if the node you find isn't a child of
the document element, e.g.

<foo>
<bar>
<Item name='AC1'/>
</bar>
</foo>

The second assert I added to your code would catch this.

Hope this helps.
 

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

Back
Top