Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Deleting element from xml doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gaurav - 19 Oct 2007 05:08 GMT
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.
Cor Ligthert[MVP] - 19 Oct 2007 05:24 GMT
Gaurav,

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

Cor
Gaurav - 19 Oct 2007 20:10 GMT
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

> Gaurav,
>
> Are you using the Adonet methods using a DataSet/DataTable, because then it
> is very simple.
>
> Cor
Cor Ligthert[MVP] - 21 Oct 2007 05:03 GMT
Gaurav,

If you first convert that set to an XML dataset.

In my idea you can even do a dataset.ReadXml(..)

Then you can use the statement

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

http://msdn2.microsoft.com/en-us/library/system.data.datacolumncollection.remove
(VS.71).aspx


Cor
Marc Gravell - 21 Oct 2007 11:13 GMT
> 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
Marc Gravell - 21 Oct 2007 11:14 GMT
> ds.Tables[0].Column[n].Remove()

Not quite sure how that solves the OP's question regarding @Locator
Marc Gravell - 19 Oct 2007 05:34 GMT
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
Marc Gravell - 19 Oct 2007 09:28 GMT
(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...)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.