473,513 Members | 3,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Manipulation Going Haywire!

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("des cendant::Item[@name='{0}']
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);
Jun 12 '07 #1
4 1142
On Jun 12, 10:56 pm, "Vai2000" <nos...@microsoft.comwrote:
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("des cendant::Item[@name='{0}'*]
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);
Seems the input is not well formatted:

second <childrennode does not have a end tag. Are you trying to make
the input in a valid format using code?

Jun 12 '07 #2
forgive me for the sample xml [I just created] XML is well formed, I was
more trying see any bug in the code?
"Aneesh Pulukkul[MCSD.Net]" <an******@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 12, 10:56 pm, "Vai2000" <nos...@microsoft.comwrote:
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("des cendant::Item[@name='{0}'*
]
","AC1"));

doc.DocumentElement.RemoveChild(deleteNode);

doc.Save(master);
Seems the input is not well formatted:

second <childrennode does not have a end tag. Are you trying to make
the input in a valid format using code?
Jun 12 '07 #3
On Jun 12, 11:32 pm, "Vai2000" <nos...@microsoft.comwrote:
forgive me for the sample xml [I just created] XML is well formed, I was
more trying see any bug in the code?

"Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.comwrote in message

news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 12, 10:56 pm, "Vai2000" <nos...@microsoft.comwrote:
Sometimes it works, sometimes it doesn't!! Can someone pinpoint the bugor
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("des cendant::Item[@name='{0}'**
]
","AC1"));
doc.DocumentElement.RemoveChild(deleteNode);
doc.Save(master);

Seems the input is not well formatted:

second <childrennode 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?

Jun 12 '07 #4
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.
--
Robert Rossney

Jun 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2281
by: Robert Kern | last post by:
I'm in the middle of writing a scientific program with a number of very, very long formulae. You can see a typical one below. What I would like to do is to parse this code to yield an AST, identify identical subtrees, replace them in the AST with a dummy variable to which I will assign the common subexpression. v21c =...
10
2429
by: Rithish | last post by:
Hi folks. A premature and redundant question maybe... But I have a TABLE problem. This is almost something like what I want to acheive. ----------------------------------------- | | | | | | Col1 | Col2 | Col3 | Col4 |
3
1858
by: Sam | last post by:
Hello, in my coding work I'm going to using a lot of matix manipulation, just basic matrix addition, multiplication, Gaussian method solving for roots, least square... But I don't know if there's a library which defines matrix and its manipulation. I know there must be one and it's somethere but I just have no experience. Thanks.
9
2581
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular ways to realize, like using cursor manipulation to backup & delete letters or otherwise to use getch to read them quetly. Maybe somebody here knows...
12
1481
by: Jeff | last post by:
Looking for your highly subjective opinions (from those of you who have been working extensively with VS 2005). I have a client that needs/wants a complete rewrite of an existing application. I will be proposing a .NET Windows application with a supporting SQL Server database. It will likely take 4-5 months before we roll out the first...
3
2146
by: Johnny Jörgensen | last post by:
I've got a serious problem. I've got Visual Studio 2005 installed, and of course I'm using the Pretty Listing formatting function. When I start up VS, everything is fine, but after a while (which can be one minute or one hour) the Pretty Listing starts acting weird. For instance, if I start writing "for...", it changes it to f(o) r" or...
0
2565
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a set of advanced tools that help Serious Traders analyze the market direction, market manipulative behavior and...
0
2337
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a...
8
2606
by: Nehil | last post by:
how can u find whether a no has consecutive zero bits either in from LSB or MSB. if they exist in between, then what to do? plz clarify.
2
2391
by: firstexact | last post by:
Hi guys, First post, hope you can help, I have done two sites www.trisant.co.uk and my own www.firstexact.co.uk. The 2 x 2 grid I originally envisaged goes haywire in Firefox as I get a 4 high by 1 wide grid. think it's something to do with my floats and clear both with a <br> in CSS..I have tried everything I can think of, the sites are fine...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.