CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Posts
    44

    XmlTextReader and Writer

    hi,

    i have the code

    Code:
    XmlTextWriter tr = new XmlTextWriter("Items.xml",null);
                tr.Formatting = Formatting.Indented;
                tr.WriteStartDocument();
    
                tr.WriteStartElement("Items");
    
                tr.WriteStartElement("books");
                tr.WriteElementString("book1","harry");
                tr.WriteElementString("book2","lotr");
                tr.WriteEndElement();
                tr.Flush();
    tr.Close();
    so it generates the xml code below

    Code:
      <?xml version="1.0" ?> 
    - <Items>
        - <books>
           <book1>harry</book1> 
           <book2>lotr</book2> 
         </books>
      </Items>
    Now i want to delete the tag <book1> and its text harry from the file. How can i do it? (after the deletion the xml file will look like this: )

    Code:
      <?xml version="1.0" ?> 
    - <Items>
        - <books>
           <book2>lotr</book2> 
         </books>
      </Items>

  2. #2
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    Re: XmlTextReader and Writer

    You can not do that (the deletion) with XmlTextWriter. Use the DOM approach for that:
    http://www.devsource.com/article2/0,1895,1934067,00.asp
    Last edited by creatorul; August 27th, 2007 at 02:44 AM.
    Bogdan

    If someone helped you then please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: XmlTextReader and Writer

    Have you tried
    Code:
    XmlTextWriter tr = new XmlTextWriter("Items.xml",null);
    tr.Formatting = Formatting.Indented;
    tr.WriteStartDocument();
     
    tr.WriteStartElement("Items");
     
    tr.WriteStartElement("books");
    tr.WriteElementString("book2","lotr");
    tr.WriteEndElement();
    tr.Flush();
    tr.Close();
    This should create exactly the code you wanted.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Jun 2007
    Posts
    44

    Re: XmlTextReader and Writer

    thanks for the website that is exactly what i want. I have one more question

    Code:
    XmlNodeList nodeList = doc.SelectNodes("//Authors");
    in the above code what is the meaning of the slashes?

    it is written that "Each slash represents a level in the element hierarchy." But i cannot understand what it means.

  5. #5
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    Re: XmlTextReader and Writer

    That is an XPath expression that selects all the Authors nodes in the document.
    http://www.w3schools.com/xpath/xpath_syntax.asp
    // Selects nodes in the document from the current node that match the selection no matter where they are
    Bogdan

    If someone helped you then please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured