将 XmlDocument 转换为字符串

发布于 2024-08-25 03:42:02 字数 610 浏览 3 评论 0原文

以下是我目前将 XMLDocument 转换为 String 的方法。

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

xmlDoc.WriteTo(xmlTextWriter);

return stringWriter.ToString();

此方法的问题是,如果我有 " ((quotes)例如

<Campaign name="ABC">
</Campaign>

上面是预期的 XML,但它返回

<Campaign name=\"ABC\">
</Campaign>

我可以执行 String.Replace "\" 但这种方法可以吗?如果 XML 本身包含 "\",则可以正常工作

Here is how I'm currently converting XMLDocument to String

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

xmlDoc.WriteTo(xmlTextWriter);

return stringWriter.ToString();

The problem with this method is that if I have " ((quotes) which I have in attributes) it escapes them.

For Instance:

<Campaign name="ABC">
</Campaign>

Above is the expected XML. But it returns

<Campaign name=\"ABC\">
</Campaign>

I can do String.Replace "\" but is that method okay? Are there any side-effects? Will it work fine if the XML itself contains a "\"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南街女流氓 2024-09-01 03:42:02

假设 xmlDoc 是一个 XmlDocument 对象 xmlDoc.OuterXml 有什么问题?

return xmlDoc.OuterXml;

OuterXml 属性 返回以下内容的字符串版本xml。

Assuming xmlDoc is an XmlDocument object whats wrong with xmlDoc.OuterXml?

return xmlDoc.OuterXml;

The OuterXml property returns a string version of the xml.

浅唱々樱花落 2024-09-01 03:42:02

没有任何报价。这只是VS调试器。尝试打印到控制台或保存到文件,您就会看到。附带说明:始终处理一次性物品:

using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    xmlDoc.WriteTo(xmlTextWriter);
    xmlTextWriter.Flush();
    return stringWriter.GetStringBuilder().ToString();
}

There aren't any quotes. It's just VS debugger. Try printing to the console or saving to a file and you'll see. As a side note: always dispose disposable objects:

using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    xmlDoc.WriteTo(xmlTextWriter);
    xmlTextWriter.Flush();
    return stringWriter.GetStringBuilder().ToString();
}
爱的故事 2024-09-01 03:42:02

如果您使用的是 XmlDocumentWindows.Data.Xml.Dom.XmlDocument 版本(例如在 UWP 应用中使用),则可以使用 yourXmlDocument.GetXml() 以字符串形式获取 XML。

If you are using Windows.Data.Xml.Dom.XmlDocument version of XmlDocument (used in UWP apps for example), you can use yourXmlDocument.GetXml() to get the XML as a string.

萌能量女王 2024-09-01 03:42:02

作为扩展方法:

public static class Extensions
{
    public static string AsString(this XmlDocument xmlDoc)
    {
        using (StringWriter sw = new StringWriter())
        {
            using (XmlTextWriter tx = new XmlTextWriter(sw))
            {
                xmlDoc.WriteTo(tx);
                string strXmlText = sw.ToString();
                return strXmlText;
            }
        }
    }
}

现在简单地使用:

yourXmlDoc.AsString()

As an extension method:

public static class Extensions
{
    public static string AsString(this XmlDocument xmlDoc)
    {
        using (StringWriter sw = new StringWriter())
        {
            using (XmlTextWriter tx = new XmlTextWriter(sw))
            {
                xmlDoc.WriteTo(tx);
                string strXmlText = sw.ToString();
                return strXmlText;
            }
        }
    }
}

Now to use simply:

yourXmlDoc.AsString()
北笙凉宸 2024-09-01 03:42:02

" 在调试器中显示为 \",但字符串中的数据是正确的,您不需要替换任何内容。尝试将字符串转储到文件中,您会发现该字符串是正确的。

" is shown as \" in the debugger, but the data is correct in the string, and you don't need to replace anything. Try to dump your string to a file and you will note that the string is correct.

猫卆 2024-09-01 03:42:02

您可以使用 xmlDoc.InnerXml 属性来获取字符串中的 xml

you can use xmlDoc.InnerXml property to get xml in string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文