<?xml version="1.0" encoding="utf-8"?> <feed xml:base="http://sds.lhp.ru/DataService.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <id>http://sds.lhp.ru/DataService.svc/UsersTDO</id><title type="text">UsersTDO</title> <updated>2016-03-21T12:50:17Z</updated><link rel="self" title="UsersTDO" href="UsersTDO" /> <entry> <id>http://sds.lhp.ru/DataService.svc/UsersTDO('002530')</id> <category term="DataService.UsersTDO" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" title="UsersTDO" href="UsersTDO('002530')" /><title /><updated>2016-03-21T12:50:17Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:ActiveDirectoryID>002530</d:ActiveDirectoryID><d:FullName>Иванов Иван Иванович</d:FullName> <d:Surname>Иванов</d:Surname><d:FirstName>Иван</d:FirstName><d:Patronymic>Иванович</d:Patronymic> <d:DepartmentName>ОЭО</d:DepartmentName><d:Position>Заведующий группой</d:Position> <d:Domain>lhp.ru</d:Domain><d:Login>IvanovII</d:Login><d:Email>sdggsd@dgdg.ru</d:Email> <d:LocalPhone>7341</d:LocalPhone><d:WorkPhone>+7(999)99999</d:WorkPhone><d:CellPhone m:null="true" /> <d:Room>10-03а</d:Room> </m:properties> </content> </entry> 

In this xml, you need to get to the values ​​of FirstName, Surname, Patronymic and DepartmentName, but something does not come out at all. Tell me, please, how to get these values.

 XDocument empDoc = XDocument.Load("UsersTDO.xml"); var users = from entries in empDoc.Descendants("entry") select new { FirstName = entries.Element("FirstName").Value }; foreach (var user in users) { Console.WriteLine(user.FirstName); } 
  • one
    Add your code to the question that you are trying to deserialize xml with. - andreycha

2 answers 2

There is a sea of ​​different paths. For example, through XDocument .

 var ns = XNamespace.Get("http://www.w3.org/2005/Atom"); var nsm = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); var nsd = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices"); var xml = XDocument.Parse(content); // или XDocument.Load(...) var entries = xml.Descendants(ns + "entry"); foreach (var entry in entries) { var props = entry.Descendants(nsm + "properties").Single(); var surname = props.Element(nsd + "Surname").Value; Console.WriteLine(surname); } 

You are generally doing more or less correctly, but you forgot to specify a namespace.


If you need to extract all the information (that is, get an adequate object model), you should probably look towards honest deserialization (declare a class tree, arrange the necessary attributes, etc.).

  • No, no, I had enough of your example with neimspaces to figure it out. In them was my stumbling block. - astral
  • @astral: That's great! Well, that sorted out. - VladD

If I were you, I would create classes from this XML using class generators from Visual Studio. Copy XML to the clipboard, and then in Visual Studio Edit -> Paste Special -> Paste XML as Classes .

But for this to be one condition - XML ​​must be valid. To validate XML for validity, you can use various online tools, such as http://www.xmlvalidation.com/ . In order to make your XML'k valid, you need to add a tag to its end:

 </feed> 

Well, then I would just deserialize this file:

 var fs = new FileStream(filename, FileMode.Open); var reader = XmlReader.Create(fs); var entry = (entry) serializer.Deserialize(reader); 

Well, and then from the resulting entry object, you would get to the desired properties:

 var firstName = entry.content.properties.FirstName; 
  • 2
    Wow, megafish. Did not know. - VladD
  • @VlaD: yes. Very convenient, especially if you need to quickly create something. The code is not bad generated, but it is better to finish it with a file. - Walter Nuss
  • Yeah, just experimenting. Thank! - VladD