[英]How to write CData in xml
i have an xml like : 我有一个像xml:
<?xml version="1.0" encoding="UTF-8"?>
<entry>
<entry_id></entry_id>
<entry_status></entry_status>
</entry>
i am writing data in it like: 我正在写数据,如:
XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status");
xnode.InnerText = "<![CDATA[ " + Convert.ToString(sqlReader["story_status"]) + " ]]>" ;
but its change "<" to "<" of CDATA. 但它改变了“<”到CDATA的“&lt;”。 Please tell me how to fill values in above xml as a CData format.
请告诉我如何将上面的xml中的值填充为CData格式。
i know that we can create CDATA like : 我知道我们可以像以下一样创建CDATA:
XmlNode itemDescription = doc.CreateElement("description");
XmlCDataSection cdata = doc.CreateCDataSection("<P>hello world</P>");
itemDescription.AppendChild(cdata);
item.AppendChild(itemDescription);
but my process is to read node of xml and change its value not to append in it. 但我的过程是读取xml的节点并更改其值而不是附加在其中。 Thanks
谢谢
Do you really need it to be in CDATA, or do you just want to get the text in there in a way which won't require extra escaping in your code? 你真的需要它在CDATA中,或者你只是想以一种不需要在代码中额外转义的方式获取文本?
InnerText
performs whatever escaping is required, so generally I'd just use InnerText
执行任何需要的转义,所以通常我只是使用
xnode.InnerText = Convert.ToString(sqlReader["story_status"]);
... but if you really want a CDATA node , you can create one yourself as per Nekresh's answer. ...但是如果你真的想要一个CDATA节点 ,你可以根据Nekresh的答案自己创建一个节点 。
If you really need a CDATA section (see Jon's answer ), you can achieve that like so: 如果你真的需要CDATA部分(参见Jon的答案 ),你可以这样做:
XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status");
XmlCDataSection cdata = xdoc.CreateCDataSection(Convert.ToString(sqlReader["story_status"]));
xnode.InnerXml = cdata.OuterXml;
This will replace the contents of xnode
, not append to it. 这将替换
xnode
的内容,而不是附加到它。
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection)
{
XmlCDataSection cdataSection = childNode as XmlCDataSection;
cdataSection.Value = newValue;
}
Use Node.InnerXml, not Node.InnerText. 使用Node.InnerXml,而不是Node.InnerText。 Node.InnerText is automatically replacing special values.
Node.InnerText自动替换特殊值。 Note that if you specify with CDATA in InnerXml, then Node.InnerText is text in CDATA.
请注意,如果在InnerXml中使用CDATA指定,则Node.InnerText是CDATA中的文本。 Example:
例:
public class Test
{
public static int Main(string[] args)
{
const string xmlTxt = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<entry>
<entry_id></entry_id>
<entry_status></entry_status>
</entry>";
TextReader treader = new StringReader(xmlTxt);
XmlReader xreader = XmlReader.Create(treader);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xreader);
XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status");
//xnode.InnerText = "<![CDATA[something]]>";
xnode.InnerXml = "<![CDATA[something]]>";
Console.WriteLine("inner text is: " + xnode.InnerText);
xdoc.Save(Console.Out); Console.WriteLine();
return 0;
}
}
Program's output: 程序的输出:
inner text is: something <?xml version="1.0" encoding="ibm852"?> <entry> <entry_id> </entry_id> <entry_status><![CDATA[something]]></entry_status> </entry>
You can use writer.WriteCData(value);
你可以使用
writer.WriteCData(value);
where writer is XmlWriter
object. 其中writer是
XmlWriter
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.