XML

G

Guest

How to delete a specific Element/node based on attrribute value??
In the below example, i need to delete Node NAME whose ID=3 ??
How do i delete it?

Ex:
<Data>
<Name id="1"></Name>
<Name id="2"></Name>
<Name id="3"></Name>
<Name id="4"></Name>
</Data>

Thanks
Vinay

--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
---------------
 
K

Kevin Spencer

It all depends on what you want to delete it FROM. XML is, after all,
extensible, and it can be used in a large variety of ways. There are a large
variety of classes for working with XM:L, and they have quite a bit in
common, and quite a bit different. So a little context would help.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
B

Bruce Barker

use an xpath query to find the element, then remove it.


XmlNode node = doc.DocumentElement.SelectSingleNode('//Name[@id=3]");
node.ParentNode.RemoveChild(node);

-- bruce (sqlwork.com)
 
G

Guest

Bruce

Thats works great !!

I have one more Q

I am getting this XML some other source.
I am not sure the attribute values are Lower case or Upper case??

when i do the xPath query, it is looking for exact match.

How to avoid the case sensitive problem??

Thanks
Vinay


--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
---------------


Bruce Barker said:
use an xpath query to find the element, then remove it.


XmlNode node = doc.DocumentElement.SelectSingleNode('//Name[@id=3]");
node.ParentNode.RemoveChild(node);

-- bruce (sqlwork.com)


vinay said:
How to delete a specific Element/node based on attrribute value??
In the below example, i need to delete Node NAME whose ID=3 ??
How do i delete it?

Ex:
<Data>
<Name id="1"></Name>
<Name id="2"></Name>
<Name id="3"></Name>
<Name id="4"></Name>
</Data>

Thanks
Vinay

--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
---------------
 
K

Kevin Spencer

XmlNode node = doc.DocumentElement;
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes.Attributes.Count > 0)
{
XmlNode n = node.ChildNodes;
for (ii = 0; ii < n.Attributes.Count; ii++)
if (n.Attributes[ii].Name.ToLower() == "id" &&
n.Attributes[ii].Value == "3")
{
node.ParentNode.RemoveChild(n);
break;
}
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.


vinay said:
Bruce

Thats works great !!

I have one more Q

I am getting this XML some other source.
I am not sure the attribute values are Lower case or Upper case??

when i do the xPath query, it is looking for exact match.

How to avoid the case sensitive problem??

Thanks
Vinay


--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
---------------


Bruce Barker said:
use an xpath query to find the element, then remove it.


XmlNode node = doc.DocumentElement.SelectSingleNode('//Name[@id=3]");
node.ParentNode.RemoveChild(node);

-- bruce (sqlwork.com)


vinay said:
How to delete a specific Element/node based on attrribute value??
In the below example, i need to delete Node NAME whose ID=3 ??
How do i delete it?

Ex:
<Data>
<Name id="1"></Name>
<Name id="2"></Name>
<Name id="3"></Name>
<Name id="4"></Name>
</Data>

Thanks
Vinay

--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
---------------
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,183
Latest member
OrderGlycoEase

Latest Threads

Top