Monday, August 10, 2009

How to change the encoding of an XML file

Following is a code sample in C#.net which shows how to change the encoding of an XML file
from code behind

string fileName = Server.MapPath("XMLFile10.xml");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(fileName);

if (xDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
{
((XmlDeclaration)xDoc.FirstChild).Encoding = "utf-8";
}

string xStr = xDoc.OuterXml;

//If you want to save it as rss file you can do this
xDoc.Save(Server.MapPath("myRSS.rss"));
//xDoc.Save(Server.MapPath("myRSS.xml"));

2 comments:

  1. This is great. Thanks kavita

    --vamsi
    -- http://webclip.in/user/vamsi

    ReplyDelete
  2. its possible to share point server

    ReplyDelete