CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Update XML

  1. #1
    Join Date
    Apr 2005
    Posts
    8

    Question Update XML

    I want to remove encoding="UTF-8" from the line: <?xml version="1.0" encoding="UTF-8"?> in an xml file using C#.

    I have seen fuctions to update other elelments but not this, can anyone please help....

    I want to update an existing xml file with element: <?xml version="1.0" encoding="UTF-8"?> to <?xml version="1.0"?>.

    Thanks!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Update XML

    Maybe this will do the trick:
    Code:
    // Load document.
    XmlDocument pDoc = new XmlDocument();
    pDoc.Load("input.xml");
    
    // Get first node (and check if it is a xml declaration).
    XmlDeclaration pDecl = pDoc.FirstChild as XmlDeclaration;
    if (pDecl == null)
    {
        // No xml declaration exists, create one.
        pDecl = pDoc.CreateXmlDeclaration("1.0", null, null);
        pDoc.InsertBefore(pDecl, pDoc.FirstChild);
    }
    else
    {
        // Modify the one we got.
        pDecl.Encoding = null;
    }
    
    // Save document.
    pDoc.Save("output.xml");
    - petter

  3. #3
    Join Date
    Apr 2005
    Posts
    8

    Red face Re: Update XML

    May you win a million dollar lottery!
    Thanks.

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