使用包含双引号的字符串属性序列化对象
本文关键字:字符串 属性 序列化 对象 包含双 | 更新日期: 2023-09-27 18:37:21
我有一个对象,它有一个字符串属性,该属性的值带有双引号。我需要序列化此对象,然后使用该 XML。我不会反序列化这个 xml。
我在 XML 文件中获取正确的内容时遇到问题。让我用一个代码示例来解释:
[Serializable]
public class Test {
[XmlElement]
public string obj { get; set; }
}
class Program {
static void Main(string[] args) {
var st ="Priority == '"1'"";
Test test = new Test();
test.obj = st;
//Serialize this object
XmlSerializer xsSubmit = new XmlSerializer(typeof(Test));
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww, new XmlWriterSettings {
OmitXmlDeclaration = true
});
var ns = new XmlSerializerNamespaces();//just to make things simpler here
ns.Add(string.Empty, string.Empty);
xsSubmit.Serialize(writer, test, ns);
//My XML
var xml = sww.ToString();
}
}
我需要我的 xml 是:
<Test><obj>Priority=="1"</obj></Test>
我现在得到:
<Test><obj>Priority=='"1'"</obj></Test>
我什至尝试使用var html = HttpUtility.HtmlEncode(st);
将字符串编码为 HTML在这种情况下,可变html
格式正确,但是在序列化时我得到:
<Test><obj>Priority==&quot;1&quot;</obj></Test>
需要一些帮助。
代码没有问题。
我实际上得到了<Test><obj>Priority=="1"</obj></Test>
,这很好。我犯的错误是我正在读取调试器上的值。当我在某处写它时,内容格式正确。
"
未转换为 "
,因为双引号在 XML 文档中被接受。在这种情况下,我可以使用它!