C#的XDocument负载多根 [英] C# XDocument Load with multiple roots

查看:169
本文介绍了C#的XDocument负载多根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有根的XML文件。我无法改变这一点。我试图解析它,但 XDocument.Load 不会这么做。我曾尝试设置 ConformanceLevel.Fragment ,但我仍然得到一个异常抛出。有没有人有一个解决的办法?

I have an XML file with no root. I cannot change this. I am trying to parse it, but XDocument.Load won't do it. I have tried to set ConformanceLevel.Fragment, but I still get an exception thrown. Does anyone have a solution to this?

我试着用的XmlReader ,但事情搞砸了,不能得到它的工作的权利。 XDocument.Load 的伟大工程,但如果我有多个根文件,它没有。

I tried with XmlReader, but things are messed up and can't get it work right. XDocument.Load works great, but if I have a file with multiple roots, it doesn't.

推荐答案

ConformanceLevel.Fragment为单一片段,你就不能换节点,以一个虚拟根在调用XDocument.Load过吗?像

ConformanceLevel.Fragment is for single fragment, can't you just wrap the nodes to one virtual root before you invoke the XDocument.Load ? Like

var myRootedXml = "<root>" + fragments + "</root>"
var doc = XDocument.Load(new StringReader(myRootedXml));

这篇关于C#的XDocument负载多根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆