problem writing xml

A

Aahz

Hello,

I need to write xml file ( using c#) with some elements inside that
actually contains html code, like this:

'Moscow <p></p><a href="www.google.com"><img
src="gallery/t20.jpg" border="0" /><br /> Red Sqare </
a>

Instead of that every time I got this:

Moscow <p></p><a
href='www.google.com'><img src='/gallery/t20.jpg' border='0' /
><br /> Red Sqare </a>

can someone please explain what I need to replace with what and where
to do that???

Thank you

P.S.
here is code:

<br /> Red Sqare </a>"

xmlfile = new XmlDocument();
xmlfilename= Server.MapPath("cities.xml");
writer = new XmlTextWriter(xmlfilename,
System.Text.Encoding.UTF8);
writer.Formatting =
System.Xml.Formatting.Indented;
writer.WriteStartDocument();
writer.WriteComment("this is a comment");
writer.WritetartElement("places");
writer.WriteAttributeString("html",city);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();

writer.Flush();
writer.Close();
 
J

Jon Skeet [C# MVP]

I need to write xml file ( using c#) with some elements inside that
actually contains html code, like this:

'Moscow <p></p><a href="www.google.com"><img
src="gallery/t20.jpg" border="0" /><br /> Red Sqare </
a>

Instead of that every time I got this:

Moscow &lt;p&gt;&lt;/p&gt;&lt;a
href='www.google.com'><img src='/gallery/t20.jpg' border='0' /
&gt;&lt;br /&gt; Red Sqare &lt;/a&gt;

can someone please explain what I need to replace with what and where
to do that???

Well, it looks like you're using the HTML as an attribute within
another element - so it's being escaped for you, correctly. If you
want to include that text directly, you should use XmlWriter.WriteRaw.

Jon
 
M

Marc Gravell

If you want to include that text directly, you should use XmlWriter.WriteRaw.

If the HTML is valid XHTML; if it isn't, then you will need a CDATA
section to wrap the HTML.

Marc
 
J

Jon Skeet [C# MVP]

If the HTML is valid XHTML; if it isn't, then you will need a CDATA
section to wrap the HTML.

Absolutely. Even if it *is* valid XHTML, the OP really needs to decide
whether he wants that data to appear as part of the XML structure, or
whether it's just text which *happens* to be XML.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top