在写入XML时更改字符串值 [英] Change the string value while writing to XML
本文介绍了在写入XML时更改字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
定义的常量字符串
const string ConstStrCheck =;
在字符串构建器对象中追加
ConstStrCheck
喜欢
StringBuilder strBody = new StringBuilder();
strBody.AppendLine(< ISAPPLICABLE>+ ConstStrCheck +适用< / ISAPPLICABLE>< STATENAME>+ ConstStrCheck +Any< / STATENAME>);
像这样进入字符串
< ISAPPLICABLE>&#4;适用< / ISAPPLICABLE>< Statename的>&安培;#4;任何< / STATENAME>
但是使用
写入XML时
XmlDocument doc = new XmlDocument();
doc.LoadXml(strBody.ToString())
像这样获取XML
< isapplicable> &# x 4;
适用< statename>&# x 4;任何
获取
x
此字符串中的额外
& ;#4;
我的尝试:
无法理解为什么会发生这种情况
请帮助我
解决方案
您可以使用XDocument class [ ^ ]。
例如:
const string ConstStrCheck =&#4;;
XElement xroot = new XElement(MyRoot);
XElement xele1 = new XElement(ISAPPLICABLE,ConstStrCheck +Applicable);
XElement xele2 = new XElement(STATENAME,ConstStrCheck +Any);
xroot.Add(xele1);
xroot.Add(xele2);
XDocument xdoc = new XDocument();
xdoc.Add(xroot);
结果:
< MyRoot >
< ISAPPLICABLE > &安培;#4;适用< / ISAPPLICABLE >
< STATENAME > &安培;#4;任何< / STATENAME >
< / MyRoot >
defined constant string
const string ConstStrCheck= "";
appending above
ConstStrCheck
in string builder object
like
StringBuilder strBody = new StringBuilder(); strBody.AppendLine("<ISAPPLICABLE>" + ConstStrCheck+ " Applicable</ISAPPLICABLE><STATENAME>" + ConstStrCheck+ " Any</STATENAME>");
getting in string like this
<ISAPPLICABLE> Applicable</ISAPPLICABLE><STATENAME> Any</STATENAME>
but while writing to XML using
XmlDocument doc = new XmlDocument(); doc.LoadXml(strBody.ToString())
getting XML like this
<isapplicable>
Applicable <statename> Any
Getting
x
Extra in this string

What I have tried:
not able to understand why its happening
Please Help me
解决方案
You can use XDocument class[^].
For example:
const string ConstStrCheck= ""; XElement xroot = new XElement("MyRoot"); XElement xele1 = new XElement("ISAPPLICABLE", ConstStrCheck + " Applicable"); XElement xele2 = new XElement("STATENAME", ConstStrCheck + " Any"); xroot.Add(xele1); xroot.Add(xele2); XDocument xdoc = new XDocument(); xdoc.Add(xroot);
Result:
<MyRoot> <ISAPPLICABLE> Applicable</ISAPPLICABLE> <STATENAME> Any</STATENAME> </MyRoot>
这篇关于在写入XML时更改字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文