473,248 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,248 software developers and data experts.

Delete attribute from XML document using XPathNodeIterator

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::
Jun 27 '08 #1
6 7650
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
Jun 27 '08 #2
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::

Jun 27 '08 #3
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
Jun 27 '08 #4
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
Jun 27 '08 #5
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::
Jun 27 '08 #6
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::
Jun 27 '08 #7

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

Similar topics

2
by: piraticman | last post by:
Hi. I have some code for a javascript jump menu... It validated with HTML 4.0 but now does not validate with XHTML strict 1.0. I have used name atributes in my code which aparantly are not...
4
by: Manuel Faux | last post by:
Hello! I used the "File Upload - Validator" and the result is like the following: Line 13, column 204: there is no attribute "border" ....irefox!" title="Get Firefox!" border="0" /></a></p>...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
1
by: soupaman | last post by:
Im trying to output some filtered xml using the xmlTextReader I know the code and commenting needs cleaned up and eventually plan to add the values to a dataset. currently what this is doing is...
2
by: Oleksandr Brovko | last post by:
Code sample below demonstrates following: We have sample extension object with 2 methods TestMethod - takes XPathNodeIterator as a parameter TestMethod2 - takes string as a parameter also we have...
7
by: jon cosby | last post by:
This code shows all values for the config node. I'm trying to retrieve the value for systrayenabled for attrib "jon". Something wrong? sXmlPath = Application.StartupPath.ToString() +...
1
by: Keith Patrick | last post by:
I'm converting over from using XmlNodes (specifically ConfigXmlNode, but the API uses XmlNode) to IXPathNavigable in some methods I have, but I'm finding in the process, that I can't seem to get...
0
by: sowmyavsh | last post by:
Below is my asp.net code which should fetch the correct answer from the xml document. how can i store the xml attribute (i.e to an object or string)? it is throwing an error object reference not...
0
by: kelvin273 | last post by:
Hi all, i'm new in this community (and in .NET programming) and i've a problem with xpathnavigator. The idea is to have an xml document with attribute editable by windows form. I write this...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.