查询第一个节点以某个声明开头的 XML

发布于 2024-10-11 03:04:39 字数 540 浏览 5 评论 0原文

我使用 linq to xml 查询 xml 没有问题,但我有这个特殊的 xml 文件,其第一个节点以声明开头。如果没有声明(例如:xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core") 我只是使用 decandents 开始查询数据。我的问题是如何查询 xml ex:使用以下格式获取 'forbiddenNamespaceDependencies' 的值:

<?xml version="1.0" encoding="utf-8"?>

<layerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core">


<layers>
<layer Id="6c1b89f1-9204-4914-a721" name="Layer1" forbiddenNamespaceDependencies="NameSpace1">
  <references>...

I have no problem using linq to xml to query xml but I have this special xml file whose first node starts with a declaration. If there was no declation ( ex: xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core")
I just use decandents to start querying data. My question is how to query xml ex: get the value of 'forbiddenNamespaceDependencies' with the following format:

<?xml version="1.0" encoding="utf-8"?>

<layerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core">


<layers>
<layer Id="6c1b89f1-9204-4914-a721" name="Layer1" forbiddenNamespaceDependencies="NameSpace1">
  <references>...

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

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

发布评论

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

评论(1

亢潮 2024-10-18 03:04:39

我不明白问题出在哪里。我获取了您的代码片段并添加了结束标记,以获得一个格式良好的 XML 文档,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<layerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core">


  <layers>
    <layer Id="6c1b89f1-9204-4914-a721" name="Layer1" forbiddenNamespaceDependencies="NameSpace1">
      <references>
        ...
      </references>>
    </layer>
  </layers>
</layerModel>

然后,以下 C# 代码

    XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
    Console.WriteLine(doc.Root.Element("layers").Element("layer").Attribute("forbiddenNamespaceDependencies").Value);

输出“forbiddenNamespaceDependencies”属性的值就好了,根元素上的命名空间声明并不重要,因为没有元素或示例中的属性位于该命名空间中。

如果您仍然遇到问题,请考虑发布足够的详细信息,以便我们重现问题。

I don't see what the problem is. I took your snippet and added closing tags to get a well-formed XML document looking like this:

<?xml version="1.0" encoding="utf-8"?>

<layerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core">


  <layers>
    <layer Id="6c1b89f1-9204-4914-a721" name="Layer1" forbiddenNamespaceDependencies="NameSpace1">
      <references>
        ...
      </references>>
    </layer>
  </layers>
</layerModel>

Then the following C# code

    XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
    Console.WriteLine(doc.Root.Element("layers").Element("layer").Attribute("forbiddenNamespaceDependencies").Value);

outputs the value of the "forbiddenNamespaceDependencies" attribute just fine, the namespace declaration on the root element does not matter as no elements or attributes in your sample are in that namespace.

If you still have problems then consider to post enough details allowing us to reproduce the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文