Escape in XML

  • Hi,

    I have an XML file that has HTML tags in some of the elements . I have written a program in C# to parse the HTML chaaracters .

    Below is a sample

    static void Main(string[] args)

    {

    string strXML = "";

    XmlTextReader textReader = new XmlTextReader("C:\\InputXML.xml");

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load(textReader);

    StringWriter writer = new StringWriter();

    xmlDoc.Save(writer);

    strXML = writer.ToString();

    strXML = strXML.Replace("<a>", "<a> ");

    strXML = strXML.Replace("<a", "<a");

    FileInfo t = new FileInfo("C:\\OutputXML.xml");

    StreamWriter Tex = t.CreateText();

    Tex.Write(strXML);

    Tex.Close();

    }}}

    But when I escape in this code as   I get an XML undeclared entity error .

    Any help appreciated .

  • Maybe I'm reading that wrong - , but two places dont' look right:

    1. if you're worried about the XML being valid - why load the stuff into an XMLdocument BEFORE you run the cleanup process? I can't help but think it bombs there

    2. your replacement commands do nothing - why?

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Sorry, the replace should have been like this

    strXML = strXML.Replace("</a>", "<"/a"> ");

    strXML = strXML.Replace("<acronym>", "<"acronym"> ")

  • Have you tried sending ith through a generic string writer to do the replacements, and then push it so XML?

    and - you need to escape the " or it will bomb (I realize the site is likely getting rid of the extra \)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply