查询第一个节点以某个声明开头的 XML
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白问题出在哪里。我获取了您的代码片段并添加了结束标记,以获得一个格式良好的 XML 文档,如下所示:
然后,以下 C# 代码
输出“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:
Then the following C# code
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.