Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Simple Linq to XML question
 

Simple Linq to XML question

Hi

Could someone help a newbie into the LINQ

I would like to retrieve the value of the <Name> and <Value> node for each <Parameter> element from this XML

The XML is loaded from a text string.

<Parameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:schemas-microsoft-com:xml-analysis">
        <Parameter>
          <Name>Datasource</Name>
          <Value xsi:type="xsd:string">[Datasource].[Datasource].&amp;[2]</Value>
        </Parameter>
        <Parameter>
          <Name>ToPeriod</Name>
          <Value xsi:type="xsd:string">[Time].[YWD].[Year].&amp;[2009].&amp;[2009 sem 47]</Value>
        </Parameter>
        <Parameter>
          <Name>Chain</Name>
          <Value xsi:type="xsd:string">[Stores].[Chain hierarchy].[Chain].&amp;[2]</Value>
        </Parameter>
        <Parameter>
          <Name>Category</Name>
          <Value xsi:type="xsd:string">[Products].[CRF Category].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>ShowTopProducts</Name>
          <Value xsi:type="xsd:string">20</Value>
        </Parameter>
        <Parameter>
          <Name>ShowTopStores</Name>
          <Value xsi:type="xsd:int">40</Value>
        </Parameter>
        <Parameter>
          <Name>DistrictManager</Name>
          <Value xsi:type="xsd:string">[Stores].[District Manager].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>AreaManager</Name>
          <Value xsi:type="xsd:string">[Stores].[Area Manager].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>Consultant</Name>
          <Value xsi:type="xsd:string">[Stores].[Consultant].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>CommercialRegion</Name>
          <Value xsi:type="xsd:string">[Stores].[Commercial region].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>PotentialGroup</Name>
          <Value xsi:type="xsd:string">[Stores].[Potential Group].[All]</Value>
        </Parameter>
        <Parameter>
          <Name>PricePeriod</Name>
          <Value xsi:type="xsd:string">[Time].[YMD].[Year].&amp;[2009].&amp;[Agosto 2009]</Value>
        </Parameter>
      </Parameters>
Should be relatively simple but I simply can't crack the nut

Thx

Erik
Erik Svensen  Tuesday, November 24, 2009 11:43 PM

Hello Erik,

Welcome to Visual Basic Language forum!

I think the most difficult part in this problem is the namespace of the XML nodes and attributes. In LINQ to XML, to retrieve certain XElement or XAttribute, we need to set their namespace correctly. For detail, please see http://msdn.microsoft.com/en-us/library/bb387093.aspx.

Here are some sample codes to retrieve the Name, Value elements and the type attribute of the Value element: (The text is loaded from a txt file)

============================================================================
Dim reader = New StreamReader("XML.txt")

Dim text = reader.ReadToEnd()

reader.Close()

Dim ns As XNamespace = "urn:schemas-microsoft-com:xml-analysis"

Dim xsi As XNamespace = "http://www.w3.org/2001/XMLSchema-instance"

Dim xsd As XNamespace = "http://www.w3.org/2001/XMLSchema"

Dim root = XElement.Parse(text)

Dim name As XElement = Nothing

Dim value As XElement = Nothing

Dim type As XAttribute = Nothing

For Each element In root.Elements(ns + "Parameter")

name = element.Element(ns + "Name")

Console.WriteLine("Name: {0}", name.Value)

value = element.Element(ns + "Value")

type = value.Attribute(xsi + "type")

Console.WriteLine("Value: {0} Type:{1}", value.Value, type.Value.Replace("xsd", xsd.ToString()))

Next
============================================================================

If you have any questions, please feel free to let me know.

Have a great day!

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Lingzhi Sun  Wednesday, November 25, 2009 1:23 AM

Hello Erik,

Welcome to Visual Basic Language forum!

I think the most difficult part in this problem is the namespace of the XML nodes and attributes. In LINQ to XML, to retrieve certain XElement or XAttribute, we need to set their namespace correctly. For detail, please see http://msdn.microsoft.com/en-us/library/bb387093.aspx.

Here are some sample codes to retrieve the Name, Value elements and the type attribute of the Value element: (The text is loaded from a txt file)

============================================================================
Dim reader = New StreamReader("XML.txt")

Dim text = reader.ReadToEnd()

reader.Close()

Dim ns As XNamespace = "urn:schemas-microsoft-com:xml-analysis"

Dim xsi As XNamespace = "http://www.w3.org/2001/XMLSchema-instance"

Dim xsd As XNamespace = "http://www.w3.org/2001/XMLSchema"

Dim root = XElement.Parse(text)

Dim name As XElement = Nothing

Dim value As XElement = Nothing

Dim type As XAttribute = Nothing

For Each element In root.Elements(ns + "Parameter")

name = element.Element(ns + "Name")

Console.WriteLine("Name: {0}", name.Value)

value = element.Element(ns + "Value")

type = value.Attribute(xsi + "type")

Console.WriteLine("Value: {0} Type:{1}", value.Value, type.Value.Replace("xsd", xsd.ToString()))

Next
============================================================================

If you have any questions, please feel free to let me know.

Have a great day!

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Lingzhi Sun  Wednesday, November 25, 2009 1:23 AM

Hello Erik,

How is the problem? If you need any further assistance, please feel free tome know.

Have a great day!

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Lingzhi Sun  Monday, November 30, 2009 12:41 AM
Hi Lingzhi,

I was able to use your solution - so thank you very much..

Could you give a short explanation why it isnt possible to call the elements via

element<Name>.value

BR
Erik
Erik Svensen  Monday, November 30, 2009 8:05 AM

Hi Erik,

To make it simpler, we need to specify the XML namespace when we try to search certain XML element or attribute names. In your XML file, the Name and Value elements are both under the namespace: �/span>urn:schemas-microsoft-com:xml-analysis�

From your second post, “element<Name>.Value� are you using the XML literals? If so, we need to imports the XML namespace in advance, http://msdn.microsoft.com/en-us/library/bb384589.aspx.

Here is the version using XML literals for your references:

======================================================================================
Imports System.IO

Imports <xmlns="urn:schemas-microsoft-com:xml-analysis">

Imports <xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Imports <xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Public Class Class1

Public Sub Test()

Dim reader = New StreamReader("NewTest.txt")

Dim text = reader.ReadToEnd()

reader.Close()

Dim root = XElement.Parse(text)

Dim name As XElement = Nothing

Dim value As XElement = Nothing

Dim type As String = Nothing

For Each element In root.<Parameter>

name = element.<Name>.Single()

Console.WriteLine(name.Value)

value = element.<Value>.Single()

type = value.@xsi:type

Console.WriteLine("Value: {0} Type:{1}", value.Value, type)

Next

End Sub

End Class
======================================================================================

Have a nice day!

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Lingzhi Sun  Monday, November 30, 2009 9:24 AM

You can use google to search for other answers

Custom Search

More Threads

• URGENT coding help required.
• Define an empty node in XML.
• User-defined messages
• Word color
• Visual Basic Express 2008: need some help.
• Populating a DataGridViewComboBoxColumn from an arraylist?
• Call a New form from within an Existing form
• Making multiple screen menus in .Net Basic
• Problems with My.Computer.Network.DownloadFile and showUI:=True
• Duplicating Files