【问题标题】:Convert XDocument to string without added /r /n将 XDocument 转换为字符串而不添加 /r /n
【发布时间】:2018-11-09 13:19:01
【问题描述】:

您好,我正在尝试找到一种方法将我的 XDocument 转换为字符串,以便我可以通过 ajax 调用将其发回。

所以我有一个 XDocument 如下

   var xml = new XDocument(
            new XElement("Contract",
                new XAttribute("version", "1.0"),
                new XElement("child", "Hello World!")));

我可以将其转换为字符串,执行以下操作

string i = xml.Document.ToString();

但是通过这样做,我的 xml 文档出现了错误,因为总体上添加了 alof ot /r/n 和很多“/”。有没有办法在没有这个附加值的情况下将 XDocument 转换为字符串?

我得到的字符串

"<Contract version=\"1.0\">\r\n  <child>Hello World!</child>\r\n</Contract>"

【问题讨论】:

    标签: .net xml linq-to-xml


    【解决方案1】:

    这应该可以解决问题:

    string i = xml.Document.ToString(SaveOptions.DisableFormatting);
    

    【讨论】:

    • 我看到仍然添加了一些反斜杠,但我认为我可以使用它。
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2010-10-29
    相关资源
    最近更新 更多