C# 如何将 XML 从 sql Xml 数据类型加载到另一个 XmlDocument,而不将其转换 <到< ETC

发布于 2025-01-14 12:38:55 字数 1857 浏览 3 评论 0 原文

下午好,

虽然这个特定问题似乎有很多答案,但似乎没有一个对我有用,而且我不确定我的问题到底出在哪里。

问题 - 我新创建的 XML 输出文件已转义“<”到& LT; 怎么能保持原样呢?我知道解析器正在做它应该做的事情,但它不是我想要的。

使用 C#,我尝试从 SQL 中获取一些 xml 类型的数据,将其加载到 XmlDocument 中并将其保存到文件中。除了 SQL 内容已被转义之外,因此与输出文件中的内容不同。

假设我抓取 SQL xml 数据并将其填充到 XmlDocument 中,如下所示:-

XmlDocument newXML = new XmlDocument();
newXML.loadXml("<root><Output>Craig</Output></root>);

然后,我创建一个输出文件实例,如下所示:-

//Create output file, root etc

XmlDocument xmlOutputDoc = new XmlDocument();` //new instance

//declaration
XmlDeclaration xmlDeclaration = xmlOutputDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
//xml declaration
XmlElement root = xmlOutputDoc.DocumentElement;` //root creation

xmlOutputDoc.InsertBefore(xmlDeclaration, root);

//create root element and attribute namespaces
XmlElement RootElement = xmlOutputDoc.CreateElement(string.Empty, "root", string.Empty);

xmlOutputDoc.AppendChild(RootElement);

然后,我将 newXML.OuterXml 添加到我的输出文档 xmlOutputDoc,如下所示:-

 //create Output location element            

    XmlElement Output = xmlOutputDoc.CreateElement("Output", string.Empty);
    RootElement .AppendChild(Output);
    XmlText OutputValue = xmlOutputDoc.CreateTextNode(newXML.OuterXml);
    Output .AppendChild(OutputValue );

因此,这会生成一个 XML文件包含以下内容:-

& lt;根& >& lt;输出& >克雷格& lt;/输出& >& lt;/根& > >

而我希望它保留:-

<root><Output>Craig</Output></root>

我尝试重新创建一个新字符串并替换 & 中的这些字符; LT;回到“<”。 我尝试过使用 WebUtility 参考。 尝试添加/指定 UTF-8 的声明。

我不明白流程的哪一部分正在进行更改,是 XmlDocument.Save 吗?我可以尝试使用 XMLWriter 和 WriteRaw 吗?

我所做的最好的是将 xml 内容包装到 CDATA 标签中,并保留我需要的内容,我只是不想/不需要那里的 CDATA 标签?

非常感谢任何帮助。

谢谢

Good afternoon,

Whilst there are seemingly many answers to this particular question, none of them seem to be working for me, and i'm unsure just exactly where my problem lies.

Problem - My newly created XML output file has escaped "<" to & lt;
How can it remain as it is? I understand the parser is doing what it should but its not what i want.

Using C#, I am trying to take some data from SQL, in the form of xml type, load it into XmlDocument and save it to a file. Except the SQL content has been escaped and is therefore different to whats in the output file.

So lets say I grab my SQL xml data and populate it in to XmlDocument like:-

XmlDocument newXML = new XmlDocument();
newXML.loadXml("<root><Output>Craig</Output></root>);

I then create an Output file instance like so:-

//Create output file, root etc

XmlDocument xmlOutputDoc = new XmlDocument();` //new instance

//declaration
XmlDeclaration xmlDeclaration = xmlOutputDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
//xml declaration
XmlElement root = xmlOutputDoc.DocumentElement;` //root creation

xmlOutputDoc.InsertBefore(xmlDeclaration, root);

//create root element and attribute namespaces
XmlElement RootElement = xmlOutputDoc.CreateElement(string.Empty, "root", string.Empty);

xmlOutputDoc.AppendChild(RootElement);

I then add newXML.OuterXml to my output document xmlOutputDoc like this:-

 //create Output location element            

    XmlElement Output = xmlOutputDoc.CreateElement("Output", string.Empty);
    RootElement .AppendChild(Output);
    XmlText OutputValue = xmlOutputDoc.CreateTextNode(newXML.OuterXml);
    Output .AppendChild(OutputValue );

So, this generates an XML file with the following:-

& lt;root& gt;& lt;Output& gt;Craig& lt;/Output& gt;& lt;/root& gt;

Whereas I want it to retain:-

<root><Output>Craig</Output></root>

I have tried to recreate a new string and replace these characters from & lt; back to "<".
Ive tried using the WebUtility reference.
Tried adding / specifying the declaration to UTF-8.

I dont understand which part of the process is doing the change, is it XmlDocument.Save? Could i try using the XMLWriter and using the WriteRaw?

Best I've done is wrap the xml contents in to a CDATA tag and that retains what i need, I just dont want/need the CDATA tags there?

Any help is greatly appreciated.

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文