开发者

query XML whose first node starts with some declaration

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"?>

<lay开发者_运维百科erModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core">


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜