Sunday 15 August 2010

c# - How to get an specific node in xml with namespaces? -


I am working to access a specific node from an XML document. I realized that this is in the form of a base namespace. Here is an example.

Enter image details here

I 'm node d Interested in getting the value of: Media URL from all the nodes (entry) And I have not completed it.

When I debug the variable Iterator 'i', then I can see that the XML also includes the default namespace again, like something:

  & lt ; The entry xmlns = "http: //schemas.microsoft.com.ado/ ..."   

and the other name space is called 'D'.

What can I do to access those specific nodes?

I have it.

  var doc = XDocument.Parse (results); String BASE_NS = "http://www.w3.org/2005/Atom"; String D = "http://schemas.microsoft.com/ado/2007/08/dataservices"; Var query = i to doc.Descendants (XName.Get ("Entry", BASE_NS)) Select new image () {Url = i.Element (XName.Get ("MediaUrl", BASE_NS)) value}; Var Results = Query.toList ();    

I use instead of XNamespace I suggest XName (Individual Priority, mainly - as I have always done with LINQ to name space on XML) To make me pre-set namespaces and to use element (n + "element name"), there is less effort to use XName.Get (though < / Code> Using XName.Get `OK if you want to do this.

If you want to get all the" Media URL "elements for each entry, I will:

  XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; var query = (i to doc.Descendants (d + "Media Url ") Select the new image () {URL = i.Value}). ToList ();   

If you want to get only one of them, then you should do something different It depends on who you wanted to get.

MediaUrl for property:

  Xnamespace d = "http://schemas.microsoft.com / Ado / 2007/08 / dataservices "; Xnamespace m =" http://schemas.microsoft.com/ado/2007/08/dataservices/metadata "var query = (among doc.Descendants (m +" Properties ") Select new image () {Url = i.Element (D + "MediaUrl"). Values}). ToList ();   

Thumbnail for Media URL:

  Xnamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; Var query = (doc.Descendants (d + "thumbnail") Select new image () {Url = i.Element (d + "MediaUrl"). Value}). ToList ();   

Here is the key name to use in order to get it in conjunction with name.

No comments:

Post a Comment