1

I'm pretty new in XML and LINQ. I've a XLM file like this:

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

I need to explore the file in two ways.

  • First I need to carge a combobox itemsource property with the Header.name
  • Second I need to make a Datagrid header with the fiels of the select layer.

I've made a linq query for take the Name property, but dosn't work.

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
1
  • I haven't errors, simply I haven't result. I'll hope to find in "e" the name of Header in XML, but the query get's nothing. Commented Nov 18, 2015 at 8:23

1 Answer 1

1

You will have to include the namespace of XML and you can find all the Header elements. This will give you the correct result:-

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}
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Work perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.