无法使用 Linq 访问 XML
我正在尝试解析以下 XML 文件,但除了根或第一个节点之外,我所做的任何操作都不会产生任何结果。
<?xml version="1.0" encoding="utf-8" ?>
<Report {attributes}>
<table1>
<Detail_Collection>
<Detail {attributes} />
<Detail {attributes} />
<Detail {attributes} />
</Detail_Collection>
</table1>
</Report>
我试图获取详细信息列表,以便获取属性值,但我尝试的所有操作都没有结果。我最近的尝试是这个...
var xml= XDocument.Load(FileDetails.FullName);
var xml1 = XDocument.Parse(xml.Root.FirstNode.ToString());
var xml3 = from e in custs.Root.Elements("Detail") select e;
var xml4 = from e in xml1.Elements("Detail") select e;
不同的尝试
var xml = XDocument.Load(FileDetails.FullName);
var root = xml.Root;
var els = root.Descendants("Detail");
上面显示在立即窗口中:
root.Descendants("详细信息") {System.Xml.Linq.XContainer.GetDescendants} 名称:空 自我:假 System.Collections.Generic.IEnumerator.Current: 无效的 System.Collections.IEnumerator.Current: 空
问题在于 Report 元素中的属性:
<Report p1:schemaLocation="Info_x0020_Tickets_x0020_Entered http://domain/ReportServer?%2fInfo+Reporting%2fInfo+Tickets+Entered&rs%3aCommand=Render&rs%3aFormat=XML&rs%3aSessionID=vcvb0p452bb3na45havjes55&rc%3aSchema=True" Name="Info Tickets Entered" textbox9="1247" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="Info_x0020_Tickets_x0020_Entered">
这是来自 SQL Server Reporting Server,我必须一一删除并找到罪魁祸首。
这是这个问题的最终解决方案 可能有更好的方法,但是一旦加载数据,您就无法轻松删除名称空间。
//load the original document
var xml = XDocument.Load(FileDetails.FullName);
//remove all the superflouos data attributes
xml.Root.RemoveAttributes();
//turn into a string
var content = xml.Root.ToString();
//remove the official namespace, there is no easy way to remove the namespace after document has been loaded, so we'll replace it
var newXmlContent = content.Replace("<Report xmlns=\"Info_x0020_Tickets_x0020_Entered\">", "<Report>");
//parse the updated string into a workable document
var newXml = XDocument.Parse(newXmlContent).Root;
这将返回一个可以正常处理的新 XML 文档。
I am trying to parse the following XML file and nothing I do results in anything but the root or the firstnode.
<?xml version="1.0" encoding="utf-8" ?>
<Report {attributes}>
<table1>
<Detail_Collection>
<Detail {attributes} />
<Detail {attributes} />
<Detail {attributes} />
</Detail_Collection>
</table1>
</Report>
I am trying to get a list of Detail so that I can get the attribute values, but everything I have tried results in no data. My latest attempt is this one...
var xml= XDocument.Load(FileDetails.FullName);
var xml1 = XDocument.Parse(xml.Root.FirstNode.ToString());
var xml3 = from e in custs.Root.Elements("Detail") select e;
var xml4 = from e in xml1.Elements("Detail") select e;
Different attempt
var xml = XDocument.Load(FileDetails.FullName);
var root = xml.Root;
var els = root.Descendants("Detail");
The above displays in the immediate window:
root.Descendants("Detail")
{System.Xml.Linq.XContainer.GetDescendants}
name: null
self: false
System.Collections.Generic.IEnumerator.Current:
null
System.Collections.IEnumerator.Current:
null
The problem is with the attributes in the Report element:
<Report p1:schemaLocation="Info_x0020_Tickets_x0020_Entered http://domain/ReportServer?%2fInfo+Reporting%2fInfo+Tickets+Entered&rs%3aCommand=Render&rs%3aFormat=XML&rs%3aSessionID=vcvb0p452bb3na45havjes55&rc%3aSchema=True" Name="Info Tickets Entered" textbox9="1247" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="Info_x0020_Tickets_x0020_Entered">
This is from SQL Server Reporting Server, I'll have to remove one by one and find the culprit.
This is the final solution to this problem
There may be a better way, but once the data is loaded you cannot easily remove the namespace.
//load the original document
var xml = XDocument.Load(FileDetails.FullName);
//remove all the superflouos data attributes
xml.Root.RemoveAttributes();
//turn into a string
var content = xml.Root.ToString();
//remove the official namespace, there is no easy way to remove the namespace after document has been loaded, so we'll replace it
var newXmlContent = content.Replace("<Report xmlns=\"Info_x0020_Tickets_x0020_Entered\">", "<Report>");
//parse the updated string into a workable document
var newXml = XDocument.Parse(newXmlContent).Root;
This returns a new XML document that can be processed normally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
custs.Root.Elements("Detail")
将返回作为根的直接子级的所有
元素(><报告>
) 元素。您需要调用
后代
。或者,您可以调用
EDIT:您的元素可能位于命名空间中(根目录中的
xmlns="..."
)。您需要向 XLINQ 询问正确命名空间中的元素:
Calling
custs.Root.Elements("Detail")
will return all<Detail>
elements that are direct children of the root (<Report>
) element.You need to call
Descendants
.Alternatively, you can call
EDIT: Your elements are probably in a namespace (
xmlns="..."
in the root).You need to ask XLINQ for the elements in the correct namespace:
这将为您提供一个可枚举的
XElements
集合,它们是 XML 中的所有Detail
元素。This will give you an enumerable collection of
XElements
that are all theDetail
elements in the XML.