XDocument.Load 丢失声明
我有一个像这样的 XML 模板文件
<?xml version="1.0" encoding="us-ascii"?>
<AutomatedDispenseResponse>
<header shipmentNumber=""></header>
<items></items>
</AutomatedDispenseResponse>
当我使用 XDocument.Load 时,由于某种原因它
<?xml version="1.0" encoding="us-ascii"?>
被删除了。
如何将文件加载到 XDocument 中而不丢失顶部的声明?
I have a XML template file like so
<?xml version="1.0" encoding="us-ascii"?>
<AutomatedDispenseResponse>
<header shipmentNumber=""></header>
<items></items>
</AutomatedDispenseResponse>
When I use XDocument.Load, for some reason the
<?xml version="1.0" encoding="us-ascii"?>
is dropped.
How do I load the file into a XDocument and not losing the declaration at the top?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑它并不是真正在加载时删除声明 - 而是当您写出文档时您丢失了它。下面是一个适合我的示例应用程序:
test.xml:
输出:
XDocument.ToString()
显示的声明不是,并且在您使用时可能会被替换XDocument.Save
因为您可能使用类似TextWriter
的东西,它已经知道它正在使用哪种编码。如果您保存到流或仅保存到文件名,根据我的经验,它会被保留。I suspect it's not really dropping the declaration on load - it's when you're writing the document out that you're missing it. Here's a sample app which works for me:
And test.xml:
Output:
The declaration isn't shown by
XDocument.ToString()
, and may be replaced when you useXDocument.Save
because you may be using something like aTextWriter
which already knows which encoding it's using. If you save to a stream or just to a filename, it's preserved in my experience.已加载。您可以使用以下命令查看它并访问它的部分内容:
It is loaded. You can see it and access parts of it using: