K
Kindler Chase
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::
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::