PC Review


Reply
Thread Tools Rate Thread

Delete attribute from XML document using XPathNodeIterator

 
 
Kindler Chase
Guest
Posts: n/a
 
      23rd Jun 2008
I'm trying to iterate through a set of nodes and then edit/delete
specific attributes using XPathNodeIterator. Adding attributes is no
problem.

My first question is how do I delete an attribute using an
XPathNodeIterator? Or should I be using something else?

In the sample that follows:
1. Grab all "Page" nodes.
2. Loop through all the nodes.
3. delete the "lastModified" attribute.
4. Will eventually just want to edit the "lastModified" attribute.

C#3/x
Sample method:
public void test()
{
XPathDocument doc = new XPathDocument( "pathToXML" );
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select( "//Section/Page" );
while( iterator.MoveNext() )
{
XPathNavigator node = iterator.Current;
string lastModifiedAttribute =
node.GetAttribute( "lastModified", "" );

// Actions will vary, but will consist of:
// 1. add attribute
// 2. delete attribute
// 3. edit attribute
}
}


Sample XML:
<?xml version="1.0"?>
<SiteMap>
<Section>
<Page nav="topic1" lastModified="20081005" />
<Page nav="topic2" lastModified="20081001" />
</Section>
<Section>
<Page nav="topic3" lastModified="20081002" />
<Section>
<Page nav="topic4" lastModified="20081004" />
<Section>
<Page nav="topic5" lastModified="20081003" />
<Page nav="topic6" lastModified="20081009" />
</Section>
</Section>
</Section>
<Section>
<Page nav="topic7" lastModified="20081012" />
<Page nav="topic8" lastModified="20081015" />
<Page nav="topic9" lastModified="20081007" />
</Section>
</SiteMap>


Thanks in advance!
::k::
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      24th Jun 2008
Kindler Chase wrote:
> I'm trying to iterate through a set of nodes and then edit/delete
> specific attributes using XPathNodeIterator. Adding attributes is no
> problem.
>
> My first question is how do I delete an attribute using an
> XPathNodeIterator? Or should I be using something else?
>
> In the sample that follows:
> 1. Grab all "Page" nodes.
> 2. Loop through all the nodes.
> 3. delete the "lastModified" attribute.
> 4. Will eventually just want to edit the "lastModified" attribute.
>
> C#3/x
> Sample method:
> public void test()
> {
> XPathDocument doc = new XPathDocument( "pathToXML" );


First problem is that XPathDocument is read only !

You need to switch to XmlDocument. It should not be a
problem to do what you want on an XmlElement.

Arne
 
Reply With Quote
 
Kindler Chase
Guest
Posts: n/a
 
      24th Jun 2008
Arne Vajhøj wrote:
> Kindler Chase wrote:
>> I'm trying to iterate through a set of nodes and then edit/delete
>> specific attributes using XPathNodeIterator. Adding attributes is no
>> problem.
>>
>> My first question is how do I delete an attribute using an
>> XPathNodeIterator? Or should I be using something else?
>>
>> In the sample that follows:
>> 1. Grab all "Page" nodes.
>> 2. Loop through all the nodes.
>> 3. delete the "lastModified" attribute.
>> 4. Will eventually just want to edit the "lastModified" attribute.
>>
>> C#3/x
>> Sample method:
>> public void test()
>> {
>> XPathDocument doc = new XPathDocument( "pathToXML" );

>
> First problem is that XPathDocument is read only !
>
> You need to switch to XmlDocument. It should not be a
> problem to do what you want on an XmlElement.
>
> Arne


Sorry for the mis-post, it actually *is* an XmlDocument - when I wrote
up the example, I mis-typed.

The problem still remains - when iterating through the nodes with the
XPathNodeiterator, I am unable to delete an attribute. Can you post an
example of iterating through and deleting an attribute?

Thanks!
::k::

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      24th Jun 2008
Arne Vajhøj wrote:
> Kindler Chase wrote:
>> I'm trying to iterate through a set of nodes and then edit/delete
>> specific attributes using XPathNodeIterator. Adding attributes is no
>> problem.
>>
>> My first question is how do I delete an attribute using an
>> XPathNodeIterator? Or should I be using something else?
>>
>> In the sample that follows:
>> 1. Grab all "Page" nodes.
>> 2. Loop through all the nodes.
>> 3. delete the "lastModified" attribute.
>> 4. Will eventually just want to edit the "lastModified" attribute.
>>
>> C#3/x
>> Sample method:
>> public void test()
>> {
>> XPathDocument doc = new XPathDocument( "pathToXML" );

>
> First problem is that XPathDocument is read only !
>
> You need to switch to XmlDocument. It should not be a
> problem to do what you want on an XmlElement.


Code snippet:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
doc.Save(Console.Out);
foreach(XmlNode n in doc.SelectNodes("//*[@lastModified]"))
{
n.Attributes.RemoveNamedItem("lastModified");
}

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      24th Jun 2008
Kindler Chase wrote:
> Arne Vajhøj wrote:
>> Kindler Chase wrote:
>>> I'm trying to iterate through a set of nodes and then edit/delete
>>> specific attributes using XPathNodeIterator. Adding attributes is no
>>> problem.
>>>
>>> My first question is how do I delete an attribute using an
>>> XPathNodeIterator? Or should I be using something else?
>>>
>>> In the sample that follows:
>>> 1. Grab all "Page" nodes.
>>> 2. Loop through all the nodes.
>>> 3. delete the "lastModified" attribute.
>>> 4. Will eventually just want to edit the "lastModified" attribute.
>>>
>>> C#3/x
>>> Sample method:
>>> public void test()
>>> {
>>> XPathDocument doc = new XPathDocument( "pathToXML" );

>>
>> First problem is that XPathDocument is read only !
>>
>> You need to switch to XmlDocument. It should not be a
>> problem to do what you want on an XmlElement.

>
> Sorry for the mis-post, it actually *is* an XmlDocument - when I wrote
> up the example, I mis-typed.


Always copy paste the real code !

> The problem still remains - when iterating through the nodes with the
> XPathNodeiterator, I am unable to delete an attribute. Can you post an
> example of iterating through and deleting an attribute?


Just posted an example.

Arne
 
Reply With Quote
 
Kindler Chase
Guest
Posts: n/a
 
      24th Jun 2008
Arne Vajhøj wrote:
>> Sorry for the mis-post, it actually *is* an XmlDocument - when I wrote
>> up the example, I mis-typed.

>
> Always copy paste the real code !


Will do

>> The problem still remains - when iterating through the nodes with the
>> XPathNodeiterator, I am unable to delete an attribute. Can you post an
>> example of iterating through and deleting an attribute?

>
> Just posted an example.


Thanks for the post/example! I'll hit it again tomorrow.

::k::
 
Reply With Quote
 
Kindler Chase
Guest
Posts: n/a
 
      24th Jun 2008
Arne Vajhøj wrote:
> Code snippet:
>
> XmlDocument doc = new XmlDocument();
> doc.LoadXml(xml);
> doc.Save(Console.Out);
> foreach(XmlNode n in doc.SelectNodes("//*[@lastModified]"))
> {
> n.Attributes.RemoveNamedItem("lastModified");
> }


Thanks Arne! It's all sorted out now. Appreciate your help.

::k::
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Can't delete an XML attribute unless you remove the'xmlns="http"//....' attribute first ?!? Why? Dude Microsoft C# .NET 0 7th Apr 2009 06:53 PM
xpathnodeiterator.getattribute gigs Microsoft C# .NET 3 1st Jun 2008 06:18 PM
Difficulty with XPathNodeIterator Michael Bray Microsoft C# .NET 2 8th Feb 2008 09:23 PM
How to remove xmlns attribute from XML document (.net) fzhang@calamos.com Microsoft C# .NET 6 8th May 2006 07:09 PM
XPathNodeIterator get outerXml? KJ Microsoft C# .NET 3 20th May 2005 05:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:58 PM.