locked
Reading XML elements and subnodes with linq RRS feed

  • Question

  • User1233494541 posted

    Hello, 

       I am trying to read an xml document with linq for databinding purposes.. The format is a bit tricky giving me problems I suspect. I can't alter the format, it comes from 3rd party video feed suppliers. 

    The format is:

    <videos>
         <video>

    <title></title>
    <description></description>
    <etc..>

    <clips>
         <clip>
             <flv>filename.flv</flv>
         </clip>
    </clips
    </video>

    <video>... </video>
    </videos>

    My code so far is this

      Dim xDoc As XDocument = XDocument.Load(Server.MapPath("~/FreeVideos/videoFeed.xml"))
    
            Dim xdata = From feed In xDoc.Root.Descendants
                Select New With {.title = feed.Element("title").Value,
                                 .Link = feed.Element("link").Value,
                                 .VidTags = feed.Element("tags").Value,
                                 .VidDesc = feed.Element("descrition").Value,
                                 .VidClipUrl = feed.Element("clip_url").Value
                                                         }
    
            vidview.DataSource = xdata
            vidview.DataBind()

    I have tried changing the method used to grab the descendants but I either get object reference not set or an empty result set in the select statement

    You can see from above that I am only trying to get the basic info from the node at this point, but I will also need to grab the flv filename from the subnode.

    Thanks for your help 

    Thursday, May 29, 2014 12:45 AM

Answers

  • User1233494541 posted

    Update:

    I have been able through trial and error get the basic information needed from the elments at the root.  I'm sure that there is a cleaner way to do it with only one select, but for the moment this partially works. Now I am trying to get to the nested element <flv>iwthin the <video><clips><clip> element.

    See original post for entire structure. Here is the code i'm using to get the first set of info:

    The last part of the select (flv) is not working. This is where I am stuck currently. Any help would be greatly appreciated.

    Had a typo, now it working as needed, I would still like to know a cleaner/faster approach to acheive this if one exists. This will be an intense section and speed/responsiveness is key.

     Dim xdata = From c In XDocument.Load(Server.MapPath("~/FreeVideos/BluePixelfeed.xml")).Descendants("videos")
                        Select c
    
     Dim vdeets = From v In xdata.Elements
                   Select New With {
                                    .title = v.Elements("title").Value,
                                    .viddesc = v.Elements("description").Value,
                                    .clip_url = v.Elements("clip_url").Value,
                                    .vtags = v.Elements("tags").Value,
                                    .flv = v.Elements("clips").Elements("clip").Elements("flv").Value
                                    }
      vidview.DataSource = vdeets
      vidview.DataBind()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 29, 2014 4:00 AM

All replies

  • User1233494541 posted

    Update:

    I have been able through trial and error get the basic information needed from the elments at the root.  I'm sure that there is a cleaner way to do it with only one select, but for the moment this partially works. Now I am trying to get to the nested element <flv>iwthin the <video><clips><clip> element.

    See original post for entire structure. Here is the code i'm using to get the first set of info:

    The last part of the select (flv) is not working. This is where I am stuck currently. Any help would be greatly appreciated.

    Had a typo, now it working as needed, I would still like to know a cleaner/faster approach to acheive this if one exists. This will be an intense section and speed/responsiveness is key.

     Dim xdata = From c In XDocument.Load(Server.MapPath("~/FreeVideos/BluePixelfeed.xml")).Descendants("videos")
                        Select c
    
     Dim vdeets = From v In xdata.Elements
                   Select New With {
                                    .title = v.Elements("title").Value,
                                    .viddesc = v.Elements("description").Value,
                                    .clip_url = v.Elements("clip_url").Value,
                                    .vtags = v.Elements("tags").Value,
                                    .flv = v.Elements("clips").Elements("clip").Elements("flv").Value
                                    }
      vidview.DataSource = vdeets
      vidview.DataBind()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 29, 2014 4:00 AM
  • Thursday, May 29, 2014 4:07 AM
  • User1231993612 posted

    Hi Kcrow,

    I would like to suggest you to avoid had code values mentioning in source code. You can mention such values in configure file and take it from there.

     

    For your current mentioned problem, I will suggest you to use below solution:

    Dim xDoc As XDocument = XDocument.Load(Server.MapPath("~/FreeVideos/videoFeed.xml"))

    Dim Data= (from c in xdoc.Element("DataElement").Elements("RootElement") select new { ComputerName= c.Element("ChieldElementName1").Value, OSVERSION= c.Element("ChieldElementName2").Value }).ToList()

    vidview.DataSource = Data vidview.DataBind()

    Hope this will help you.

    Thursday, May 29, 2014 5:38 AM
  • User1233494541 posted

    Thank you for your response. What code values are you refering to specifically?

    Thursday, May 29, 2014 6:27 AM