Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include CData using LINQ to XML?

I want to record to a XML with below those codes in ASP.Net.However,I want to add <![[CDATA]]> in the fifth element. when I making it as shown below, it's creating ""&"bt;"instead of > character and ""&"lt;"instead of < character to XML. How to get rid of that problem?

Code:

XElement xml = new XElement("photo",
        new XElement("thumbnail", TextBox1.Text),
        new XElement("filename", TextBox2.Text),
        new XElement("baslik1", TextBox3.Text),
        new XElement("baslik2", TextBox4.Text),
        new XElement("description","<>"+TextBox5.Text),
        new XElement("link", TextBox6.Text),
        new XElement("fiyat1", TextBox7.Text),
        new XElement("indorani", TextBox8.Text));

XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/satislar.xml"));

doc.Root.Add(xml);

doc.Save(Server.MapPath("~/App_Data/satislar.xml"));

Response.Write("kayıt eklendi");
new XElement("description","<>"+TextBox5.Text),
like image 956
MaxCoder88 Avatar asked Jul 22 '11 00:07

MaxCoder88


People also ask

How do I add CDATA to XML?

CDATA sections may be added anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string " <! [CDATA[ " and end with the string " ]]> ".

Can we use LINQ for XML?

LINQ to XML provides an in-memory XML programming interface that leverages the . NET Language-Integrated Query (LINQ) Framework. MSDN Says, LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the . NET Framework programming languages.

What is CDATA in XML example?

The term CDATA means, Character Data. CDATA is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup. The predefined entities such as &lt;, &gt;, and &amp; require typing and are generally difficult to read in the markup. In such cases, CDATA section can be used.

Should I use CDATA in XML?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document. There are two methods to ensure that an XML file is well-formed.


1 Answers

Try this:

new XElement("description",
    new XCData("<>" + TextBox5.Text)),

in place of your current

new XElement("description", "<>" + TextBox5.Text),

line.

like image 108
Tim Avatar answered Sep 19 '22 14:09

Tim