无法使用 Linq 访问 XML

发布于 2024-10-07 00:50:00 字数 2351 浏览 3 评论 0原文

我正在尝试解析以下 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-10-14 00:50:00

调用 custs.Root.Elements("Detail") 将返回作为根的直接子级的所有 元素(><报告>) 元素。

您需要调用后代

或者,您可以调用

custs.Root.Element("table1").Element("Detail_Collection").Elements("Detail")

EDIT:您的元素可能位于命名空间中(根目录中的xmlns="...")。
您需要向 XLINQ 询问正确命名空间中的元素:

XNamespace ns = "Info_x0020_Tickets_x0020_Entered";
var els = root.Descendants(ns + "Detail");

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

custs.Root.Element("table1").Element("Detail_Collection").Elements("Detail")

EDIT: Your elements are probably in a namespace (xmlns="..." in the root).
You need to ask XLINQ for the elements in the correct namespace:

XNamespace ns = "Info_x0020_Tickets_x0020_Entered";
var els = root.Descendants(ns + "Detail");
一花一树开 2024-10-14 00:50:00
var xml= XDocument.Load(FileDetails.FullName);
var detailList = xml.Root.Decendants("Detail");

这将为您提供一个可枚举的 XElements 集合,它们是 XML 中的所有 Detail 元素。

var xml= XDocument.Load(FileDetails.FullName);
var detailList = xml.Root.Decendants("Detail");

This will give you an enumerable collection of XElements that are all the Detail elements in the XML.

人间☆小暴躁 2024-10-14 00:50:00
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
 <Report foo=""bar"">
     <table1>
         <Detail_Collection>
            <Detail foo=""bar"" />
            <Detail foo=""bar"" />
            <Detail foo=""bar"" />
         </Detail_Collection>
     </table1>
 </Report>";

        var doc = XDocument.Parse(xml);
        foreach (var desc in doc.Root.Descendants("Detail"))
        {
            Console.WriteLine(desc.Attribute("foo").Value);
        }
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
 <Report foo=""bar"">
     <table1>
         <Detail_Collection>
            <Detail foo=""bar"" />
            <Detail foo=""bar"" />
            <Detail foo=""bar"" />
         </Detail_Collection>
     </table1>
 </Report>";

        var doc = XDocument.Parse(xml);
        foreach (var desc in doc.Root.Descendants("Detail"))
        {
            Console.WriteLine(desc.Attribute("foo").Value);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文