• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Anyone know Linq to XML?

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Anyone know Linq to XML?

    I need to parse some monster XML docs and have had a play with Linq to XML this morning - not used it before but looks good!

    Can anyone point me in the right direction here, say i've got a simple XML doc:

    Code:
    <?xml version="1.0" ?> 
    <SomeMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Versions>
    <Version name="TheVersionName" description="TheVersionDesc">
    <Properties>
    <Property name="Version Name">TheVersionName</Property>
    <Property name="Version Status">Working</Property>
    </Properties>
    <Hierarchies>
    <Hierarchy name="Period" description="Period Hierarchy">
    <Properties>
    <Property name="Hier Name">Period</Property>
    <Property name="Hierarchy Status">Working</Property> 
    </Properties>
    </Hierarchy>
    <Hierarchy name="Currency" description="Currency Hierarchy">
    <Properties>
    <Property name="Hier Name">Currency</Property> 
    <Property name="Hierarchy Status">Working</Property>
    </Properties>
    </Hierarchy>
    </Hierarchies>
    </Version>
    </Versions>
    </SomeMetadata>
    I want to find out what the Version attributes are so i try:

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim TheXmlDoc As New XDocument
            TheXmlDoc = XDocument.Load(Server.MapPath("XML/Test.xml"))
    
            Dim versions = From version In TheXmlDoc.Descendants("Version") _
                              Select VersionName = version.Attribute("name").Value, _
            VersionDesc = version.Attribute("description").Value, _
            VersionStatus = version.Element("Properties").Value
    
            For Each item In versions
                Response.Write("Version Name: " & item.VersionName & "<br />Version Description: " & item.VersionDesc _
                                & "<br />Version Status: " & item.VersionStatus)
            Next
    End Sub
    This returns:

    Version Name: TheVersionName
    Version Description: TheVersionDesc
    Version Status: TheVersionNameWorking


    What should my incorrect line 'VersionStatus = version.Element("Properties").Value' be so i can just grab the property value: name="Version Status" and get Version Status: Working? i.e i need to filter on the name value of the property attributes.

    Ta.

    #2
    Ahhh Lambda expressions.

    Bit of a mouthful in programming terms, but try...


    VersionStatus = version.Element("Properties").Elements("Property") .Where(Function(i) i.Attribute("name").Value = "Version Status").Value

    Comment


      #3
      Originally posted by lightng View Post
      Ahhh Lambda expressions.

      Bit of a mouthful in programming terms, but try...


      VersionStatus = version.Element("Properties").Elements("Property") .Where(Function(i) i.Attribute("name").Value = "Version Status").Value
      That only fookin works!!

      You are a genius Mr Lightng and wasted in permiedom!!

      Comment


        #4
        Originally posted by lightng View Post
        Ahhh Lambda expressions.

        Where(Function(i) i.Attribute("name").

        Does the shorter version of Where(i=>i blah blah blah) still work?

        Comment


          #5
          Originally posted by Weltchy View Post
          Does the shorter version of Where(i=>i blah blah blah) still work?
          In C#, yes.

          Comment


            #6
            When you say monster XML docs, how big are we talking?

            Linq To XML doesn't stream the XML, meaning if your docs are 100mb in size (the infoset is approx 10x larger in memory), and your system is attempting to open a few of these at once... out of memory exceptions are likely.

            Similarily, if you are trying to process 100 xml docs that are only 1mb each, thats roughly 1gb in memory.

            Just something to think about in case it affects you.

            TM

            Comment


              #7
              Originally posted by themistry View Post
              When you say monster XML docs, how big are we talking?
              Quite small really, not much data.

              The monster bit is the data itself which is a load of separate interrelated hierarchies which i need to piece back together. This is no fun so i thought i'd have a play with Linq to see if it can help in other areas.

              Cheers for the heads up though - whilst the data is currently viewed as minimal, we were planning to test with a very large dataset to see if it copes so i'll see if that causes issues.

              Comment


                #8
                LINQ is good but I tend to use the old .NET XML libraries to manipulate my XML. LINQ does enable developers to use the same methods to work with data whatever source it comes from but I think if developers are used to using XPATH, they'll tend to favour that over LINQ. One thing I like about XPATH is that you can avoid hard-coding queries; I also find it very readable.

                I use LINQ to objects (business objects) a lot but I'm personally not that keen on LINQ to XML or LINQ to SQL.

                Comment


                  #9
                  Was just Googling performance following themistry's post as was thinking i don't know how this data will grow:

                  http://www.nearinfinity.com/blogs/pa...linq_to_sql_vs

                  Guess i'll probably stick to the Reader just in case.

                  Comment


                    #10
                    Originally posted by Durbs View Post
                    Was just Googling performance following themistry's post as was thinking i don't know how this data will grow:

                    http://www.nearinfinity.com/blogs/pa...linq_to_sql_vs

                    Guess i'll probably stick to the Reader just in case.
                    Using the XmlReader also has its challenges. Of the top of my head (I may have this confused with something else) it's a forward only reader, so xpaths that require lookups across axes, counts etc are not possible.

                    It's also harder to work with. I'd suggest sticking with your Linq direction, but run through some larger example files to ensure its ok.

                    Most XML files I have worked with are sub 100Kb, so unless you are doing anything extraordinary, i'd suggest saving yourself the hastle and using Linq to XML.

                    TM

                    Comment

                    Working...
                    X