Delete attribute from XML document using XPathNodeIterator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kindler Chase

    Delete attribute from XML document using XPathNodeIterator

    I'm trying to iterate through a set of nodes and then edit/delete
    specific attributes using XPathNodeIterat or. Adding attributes is no
    problem.

    My first question is how do I delete an attribute using an
    XPathNodeIterat or? 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 "lastModifi ed" attribute.
    4. Will eventually just want to edit the "lastModifi ed" attribute.

    C#3/x
    Sample method:
    public void test()
    {
    XPathDocument doc = new XPathDocument( "pathToXML" );
    XPathNavigator nav = doc.CreateNavig ator();
    XPathNodeIterat or iterator = nav.Select( "//Section/Page" );
    while( iterator.MoveNe xt() )
    {
    XPathNavigator node = iterator.Curren t;
    string lastModifiedAtt ribute =
    node.GetAttribu te( "lastModifi ed", "" );

    // 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="2 0081005" />
    <Page nav="topic2" lastModified="2 0081001" />
    </Section>
    <Section>
    <Page nav="topic3" lastModified="2 0081002" />
    <Section>
    <Page nav="topic4" lastModified="2 0081004" />
    <Section>
    <Page nav="topic5" lastModified="2 0081003" />
    <Page nav="topic6" lastModified="2 0081009" />
    </Section>
    </Section>
    </Section>
    <Section>
    <Page nav="topic7" lastModified="2 0081012" />
    <Page nav="topic8" lastModified="2 0081015" />
    <Page nav="topic9" lastModified="2 0081007" />
    </Section>
    </SiteMap>


    Thanks in advance!
    ::k::
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Delete attribute from XML document using XPathNodeIterat or

    Kindler Chase wrote:
    I'm trying to iterate through a set of nodes and then edit/delete
    specific attributes using XPathNodeIterat or. Adding attributes is no
    problem.
    >
    My first question is how do I delete an attribute using an
    XPathNodeIterat or? 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 "lastModifi ed" attribute.
    4. Will eventually just want to edit the "lastModifi ed" 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

    Comment

    • Kindler Chase

      #3
      Re: Delete attribute from XML document using XPathNodeIterat or

      Arne Vajhøj wrote:
      Kindler Chase wrote:
      >I'm trying to iterate through a set of nodes and then edit/delete
      >specific attributes using XPathNodeIterat or. Adding attributes is no
      >problem.
      >>
      >My first question is how do I delete an attribute using an
      >XPathNodeItera tor? 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 "lastModifi ed" attribute.
      >4. Will eventually just want to edit the "lastModifi ed" 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
      XPathNodeiterat or, I am unable to delete an attribute. Can you post an
      example of iterating through and deleting an attribute?

      Thanks!
      ::k::

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Delete attribute from XML document using XPathNodeIterat or

        Arne Vajhøj wrote:
        Kindler Chase wrote:
        >I'm trying to iterate through a set of nodes and then edit/delete
        >specific attributes using XPathNodeIterat or. Adding attributes is no
        >problem.
        >>
        >My first question is how do I delete an attribute using an
        >XPathNodeItera tor? 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 "lastModifi ed" attribute.
        >4. Will eventually just want to edit the "lastModifi ed" 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(Consol e.Out);
        foreach(XmlNode n in doc.SelectNodes ("//*[@lastModified]"))
        {
        n.Attributes.Re moveNamedItem(" lastModified");
        }

        Arne

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: Delete attribute from XML document using XPathNodeIterat or

          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 XPathNodeIterat or. Adding attributes is no
          >>problem.
          >>>
          >>My first question is how do I delete an attribute using an
          >>XPathNodeIter ator? 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 "lastModifi ed" attribute.
          >>4. Will eventually just want to edit the "lastModifi ed" 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
          XPathNodeiterat or, I am unable to delete an attribute. Can you post an
          example of iterating through and deleting an attribute?
          Just posted an example.

          Arne

          Comment

          • Kindler Chase

            #6
            Re: Delete attribute from XML document using XPathNodeIterat or

            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
            >XPathNodeitera tor, 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::

            Comment

            • Kindler Chase

              #7
              Re: Delete attribute from XML document using XPathNodeIterat or

              Arne Vajhøj wrote:
              Code snippet:
              >
              XmlDocument doc = new XmlDocument();
              doc.LoadXml(xml );
              doc.Save(Consol e.Out);
              foreach(XmlNode n in doc.SelectNodes ("//*[@lastModified]"))
              {
              n.Attributes.Re moveNamedItem(" lastModified");
              }
              Thanks Arne! It's all sorted out now. Appreciate your help.

              ::k::

              Comment

              Working...