Deleting element from xml doc

G

Guest

Hello,

I am stuck trying to apply the following logic to the below xml:
<?xml version='1.0' encoding='ISO-8859-1'?>
<Collection>
<Book Id='1' Locator='Yes'>
<Title>Principle of Relativity</Title>
<Author>Albert Einstein</Author>
<Genre>Physics</Genre>
</Book>
<Book Id='2'>
<Title>Cosmos</Title>
<Author>Carl Sagan</Author>
<Genre>Cosmology</Genre>
</Book>
</Collection>

Read the "Locator" attribute from the Book element, in case it is "Yes"
.....delete the entire book element, so that the o/p now looks like:

<?xml version='1.0' encoding='ISO-8859-1'?>
<Collection>
<Book Id='2'>
<Title>Cosmos</Title>
<Author>Carl Sagan</Author>
<Genre>Cosmology</Genre>
</Book>
</Collection>

It would be very helpful if somebody can provide a little code on how to
achieve this?

Thanks.
 
C

Cor Ligthert[MVP]

Gaurav,

Are you using the Adonet methods using a DataSet/DataTable, because then it
is very simple.

Cor
 
M

Marc Gravell

What have you tried? Go on, give it a go ;-p

If the above truly is indicative of the xml (in terms of size, I
mean), then XmlDocument is suitable, and pointers (preudo-code) would
be:
* load it into a XmlDocument
* Collection is the DocumentElement
* which has 2 child-nodes called Book that you can loop over [1]
* although each Book is an XmlNode, it is actually an XmlElement which
makes getting attributes easier
* each Book has an Locator attribute
* a parent (Collection) can remove a child (Book)

The code is 8 simple lines to do - but it is worth having a go
yourself first...

[1]: note that a common problem is removing items from a list you are
currently iterating, as it often breaks the enumerator. There are
several workarounds... so if this is the problem, say so ;-p

If the data volume is higher (i.e. there are 10000 Book elements) then
another option would be XmlReader, but this is a little trickier.

XSLT is a 3rd option depending on scenario.

Marc
 
M

Marc Gravell

(or about 3 lines if you use the xpath/xquery to look at the
XmlDocument, but I was hoping to lead into that once you have it
working the "long way", as it would help explain what the xpath/xquery
means...)
 
G

Guest

Cor,

I am not using ADO.NET. Can't figure out how to get the desired o/p.... can
u plz provide some code/pointers?

Cheers,
Gaurav
 
M

Marc Gravell

If you first convert that set to an XML dataset.

Sorry, Cor - but personally I think that a DataSet is entirely the
wrong tool for handling xml... just my opinion...
the following seems to more closely express the intent:

XmlDocument doc = new XmlDocument();
doc.Load("path.xml");
foreach (XmlNode node in doc.SelectNodes("/Collection/
Book[@Locator='Yes']")) {
node.ParentNode.RemoveChild(node);
}
doc.Save("path2.xml");

The arg to SelectNodes reads "find all root->Collection->Book elements
where there is a Locator attribute with the value of yes"; we iterate
that, for each one removing it from it's parent.

Marc
 
M

Marc Gravell

ds.Tables[0].Column[n].Remove()

Not quite sure how that solves the OP's question regarding @Locator
 

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

Similar Threads

XmlReader error Root Element is missing 3
XML - read thru the values 3
Edit XML document 4
XmlDocument.SelectNodes 1
Xpath On selected node?? 2
Xml and ReadElementString 1
XML Example 5
Bug in XSLT with c# ? 1

Top