2

我正在使用 XmlTextWriter 从 C# 应用程序生成 XML。初始输入将采用这种格式, 1" ,但我可以用任何东西替换它。我需要结束1"但我不断得到1"

C#

    xml.WriteStartElement("data");
    xml.WriteAttributeString("type", "wstring");
    xml.WriteString("1"");
    xml.WriteEndElement();

哈!当我通过这里需要的 XML 时,它会将其转换为 1"。但我真正需要展示的是1"代码第 3 行中的实际值。

我还需要使用 / 和 Ø。我该怎么做,谢谢。

4

1 回答 1

5

I need to end up with 1" but I keep getting 1"

That's because you're trying to do the XML API's job for it.

Your job is to provide just text - its job is to handle escaping and anything else XML needs to do.

So you should just use

xml.WriteString("1\"");

... where the backslash is just for the sake of C#'s string literal handling, and has nothing to do with XML. The logical value you're trying to write out is a 1 followed by a double-quote. Whether it's escaped as " or not should be irrelevant to anything processing it. If you've got something which is over-sensitive, you should fix that.

If you desperately need this (and I would again strongly urge you not to), try:

xml.WriteString("1");
xml.WriteEntityRef("quot");

(I'd also urge you to use LINQ to XML unless you're in a situation where you really need to use XmlWriter. It'll make your life significantly simpler.)

于 2012-12-07T18:42:46.450 回答