-1

So I have xml from a url that looks like this:

<restaurantFoodImpls>
 <RestaurantFood Price="1689.7594" ID="426" Description="quis egreddior glavans brevens, si eggredior. vobis e fecundio, fecundio, et quoque nomen gravum parte volcans">
  <foodItem Name="Frances93" ID="548"/>
  <restaurant Name="Alana59" PhoneNumber="7954016342" MobileNumber="372206-3626" LastName="Hickman" ID="1" FirstName="Gabrielle"/>
 </RestaurantFood>
 <RestaurantFood Price="14.225095" ID="520" Description="in plorum egreddior plorum e pladior in linguens essit. novum habitatio Versus plurissimum volcans linguens estum.">   
  <foodItem Name="Frances93" ID="548"/>
  <restaurant Name="Alana59" PhoneNumber="7954016342" MobileNumber="372206-3626" LastName="Hickman" ID="1" FirstName="Gabrielle"/>
 </RestaurantFood>
</restaurantFoodImpls>

How to parse it into objects using C#?
I have tried using the deserializer but my problem is that I want the properties of the elements read from the attributes in XML, and I couldn't get them.

Bazzz
  • 26,427
  • 12
  • 52
  • 69
Ammar
  • 169
  • 1
  • 5
  • 15
  • 4
    using a deserializer, search for it in your favourite search engine. – Bazzz Jun 01 '13 at 18:53
  • I tried to use it ..... actually my problem that I want the properties of the elements and I couldn't get them – Ammar Jun 01 '13 at 18:55
  • Check out [this question](http://stackoverflow.com/questions/670563/linq-to-read-xml). Also, look at the answers to the questions under the Related section to the right of this screen. – DOK Jun 01 '13 at 18:56
  • read about [how-to-deserialize-xml-to-object][1] follow the steps there [1]: http://stackoverflow.com/questions/10518372/how-to-deserialize-xml-to-object – Mzf Jun 01 '13 at 18:57
  • @Ammar, had you put that information in the question you would have gotten several upvotes, whereas now you have several downvotes. Merely because you formulated your question poorly in regards to the situation where you have an actual problem that fits this website. – Bazzz Jun 03 '13 at 07:03

1 Answers1

2
var stream = File.Open(filename, FileMode.Open);
XmlSerializer ser = new XmlSerializer(typeof(RestaurantFoodImpls));
var result = ser.Deserialize(stream) as RestaurantFoodImpls;

public class FoodItem
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string ID { get; set; }
}

public class Restaurant
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string PhoneNumber { get; set; }
    [XmlAttribute]
    public string MobileNumber { get; set; }
    [XmlAttribute]
    public string LastName { get; set; }
    [XmlAttribute]
    public string ID { get; set; }
    [XmlAttribute]
    public string FirstName { get; set; }
}

public class RestaurantFood
{
    [XmlAttribute]
    public string Price { get; set; }
    [XmlAttribute]
    public string ID { get; set; }
    [XmlAttribute]
    public string Description { get; set; }
    [XmlElement("foodItem")]
    public FoodItem foodItem { get; set; }
    [XmlElement("restaurant")]
    public Restaurant restaurant { get; set; }
}

[XmlRoot("restaurantFoodImpls")]
public class RestaurantFoodImpls
{
    [XmlElement("RestaurantFood")]
    public List<RestaurantFood> RestaurantFood { get; set; }
}
I4V
  • 34,891
  • 6
  • 67
  • 79
  • thank you very much l4V ..... just a little more help please .... the xml resault I want to get it from a web service using a url ..... what is the changes that I have to do for the above code – Ammar Jun 01 '13 at 20:32
  • 1
    @Ammar Sorry i don't get your question. If what you are asking is *how can i download this xml*, then you can use *WebClient* or *HttpWebRequest* – I4V Jun 01 '13 at 20:44
  • so sorry for misunderstanding ...... just be patient for a little ..... I have a server side implemented with java .... i am trying to consume the web services from the server using C# .... all I got is the URL "http://localhost:8080/last2/eattel/restaurants/1/foods"..... and I want to put the XML result into objects so I can use it if you please help me a little more – Ammar Jun 01 '13 at 20:49
  • @l4V so sorry for misunderstanding ...... just be patient for a little ..... I have a server side implemented with java .... i am trying to consume the web services from the server using C# .... all I got is the URL "localhost:8080/last2/eattel/restaurants/1/foods";..... and I want to put the XML result into objects so I can use it if you please help me a little more – Ammar Jun 01 '13 at 21:12