本机 c# 函数可满足 xml

Native c# functions to desirabilia an xml

我有一个像这样的字符串:

<Cols><Col idx="C1" Name="Speed" Type="Int" /><Col idx="C2" Name="Time" Type="Decimal" /></Cols>

我需要转换为 XDocument:

结果 xml 是:

<Cols>
    <Col idx="C1" Name="Speed" Type="Int" />
    <Col idx="C2" Name="Time" Type="Decimal" />
</Cols>

我可以用一个简单的替换逻辑来完成,然后加载它,但是在 C# 中是否有任何内置函数?

您可以使用 System.IO.WebUtilitySystem.Web.HttpUtility

string s = "&lt;Cols&gt;&lt;Col idx=&quot;C1&quot; Name=&quot;Speed&quot; Type=&quot;Int&quot; /&gt;&lt;Col idx=&quot;C2&quot; Name=&quot;Time&quot; Type=&quot;Decimal&quot; /&gt;&lt;/Cols&gt;";
var xml = WebUtility.HtmlDecode(s); //OR HttpUtility.HtmlDecode(s);
var xDcoc = XDocument.Parse(xml);