Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2006, 06:07 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
 
Old March 6th, 2006, 06:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The XML declaration is part of the serialized XML, it's not normally represented as part of the in-memory data model that your C# program sees, because "encoding" has no meaning there. Look at the class you use to serialize the XML (e.g an XmlTextWriter) and see if there is a method to set the encoding on the output.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 7th, 2006, 11:10 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is a declaration class in C#, code below will do it....

// 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");






Similar Threads
Thread Thread Starter Forum Replies Last Post
XML update (ntext column) tusharkale SQL Server 2000 11 July 24th, 2008 01:25 PM
XML update tusharkale SQL Language 0 July 11th, 2008 09:13 AM
how to update a xml file billbillbill XML 2 April 11th, 2007 08:23 AM
update xml file using vbs thesun99 XML 0 March 21st, 2005 02:32 AM
DataGrid .. XML .. Errors to update sean XML 2 July 10th, 2003 09:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.