我是XML和LINQ的新手。 我是这样的XLM文件:
<?xml version="1.0" encoding="utf-8"?>
<Headers xmlns="http://tempuri.org/GridLayerSchema.xsd">
<Header>
<Name>Layer0</Name>
<Fields FieldID="FieldID0" FieldName="FieldNameAll" FieldPosition="0"FieldPositionStart="0" FieldLenght="254" FieldEnable="true" />
</Header>
<Header>
<Name>Layer1</Name>
<Fields FieldID="FieldID0" FieldName="JetPosition" FieldPosition="0" FieldPositionStart="0" FieldLenght="14" FieldEnable="true" />
<Fields FieldID="FieldID1" FieldName="Owner" FieldPosition="1" FieldPositionStart="14" FieldLenght="14" FieldEnable="true" />
<Fields FieldID="FieldID2" FieldName="Item" FieldPosition="2" FieldPositionStart="28" FieldLenght="3" FieldEnable="true" />
</Header>
</Headers>
我需要以两种方式探索文件。
我已经使用了一个linq查询来获取Name属性,但是dosn没有工作。
Dim xdoc As XDocument = XDocument.Load(My.Application.Info.DirectoryPath & "\Layers\Layers.xml")
Dim query = From el In xdoc...<Headers>
Select New Header With {.Name = el.@Name}
For Each e In query
HeadersCollection.Add(e)
Next
答案 0 :(得分:0)
您必须包含XML的命名空间,并且您可以找到所有Header
元素。这将为您提供正确的结果: -
Dim ns As XNamespace = "http://tempuri.org/GridLayerSchema.xsd"
Dim query = From e1 In xdoc.Root.Elements(ns + "Header")
Select New With {.Name = e1.Element(ns + "Name").Value}