-1

I have xml like that:

<Person>
  <Person>
    <Name>Asd</Name>
    <Surname>Dsa/Surname>
    <City>ASdasd</City>
  </Person>
  <Person>
    <Name>Asadas</Name>
    <Surname>Dsadsad</Surname>
    <City>dsadsa</City>
  </Person>
</Person>

Class Person:

  public class Person
    {
        public string Name{ get; set; }
        public string Surname{ get; set; }
        public string City { get ; set; }

    }

Function: public static void

SendTheLoadedPerson(ObservableCollection<ObservableCollection<Person>> list)
{
XmlRootAttribute oRootAttr = new XmlRootAttribute();
            XDocument doc = XDocument.Parse(path);
            var pepoleList= (from r in doc.Root.Elements("Person")
                         select new Person()
                         {
                             Name = (string)r.Element("Name"),
                             Surname = (string)r.Element("Surname"),
                             City = (string)r.Element("City")

                         }).ToList();
}

I would like to add every person to the list ObservableCollection> list But once I have no idea how to do that, than moreover the pepoleList returns empty Please could you tell me any tip? My case is diffrent than others because I have ObservableCollection> list others just have list of object

Elgahir
  • 57
  • 2
  • 8
  • Possible duplicate of [How to deserialize xml to object](https://stackoverflow.com/questions/10518372/how-to-deserialize-xml-to-object) – Sparrow Nov 29 '17 at 20:50
  • Nope, I have checked it, also like all stackoverflow and I didnt find anything similar to my case – Elgahir Nov 29 '17 at 20:53
  • Is that the exact xml you have? First person's `Surname` element is malformed. – Sergei Z Nov 29 '17 at 20:57
  • this is not valid xml. Node `Surname` in first name is not valid, collection should be `Persons` (plural) etc... – Nino Nov 29 '17 at 20:59
  • If by "peopleList returns empty" you mean that the method does not return anything, then that's because it's `void`. (In addition, `list` and `oRootAttr` are never used). – fuglede Nov 29 '17 at 21:03
  • I changed the first element, my example is a bit different, but I have the same code, so I wrote it myself, but I have an object with almost identical variables – Elgahir Nov 29 '17 at 21:04
  • I know that - i just want to update the list, prevosly I have that in txt and everything works fine :( – Elgahir Nov 29 '17 at 21:05
  • If you have the XML string that starts with a "Persons" node you can do it using a DataTable object: https://stackoverflow.com/questions/7801646/xml-string-to-datatable-in-c-sharp then convert that into an IEnumerable list: https://stackoverflow.com/questions/7694029/how-to-convert-a-data-table-to-a-list-of-strongly-typed-objects-in-c-sharp-using – Ph0b0x Nov 29 '17 at 21:24
  • Why do you have a `` node underneath a `` node? – mason Nov 29 '17 at 21:27

1 Answers1

0

Actually, I think there is a typo in your XML which makes it tricky to parse properly:

<!-- Persons maybe, not Person... -->
<Persons> 
  <Person>
    <Name>Asd</Name>
    <Surname>Dsa/Surname>
    <City>ASdasd</City>
  </Person>
  <Person>
    <Name>Asadas</Name>
    <Surname>Dsadsad</Surname>
    <City>dsadsa</City>
  </Person>
</Persons>

Starting from that assumption, define your classes (including the root tag):

[XmlRoot("Persons")]
public class Persons
{
    [XmlElement("Person")]
    public List<Person> Persons { get; set; }
}

public class Person
{
    [XmlElement("Name")]
    public String Name { get; set; }

    [XmlElement("Surname")]
    public String Surname { get; set; }

    [XmlElement("City")]
    public String City { get; set; }
}

and deserialize your file:

Persons persons = null;
XmlSerializer serializer = new XmlSerializer(typeof(Persons));

using (FileStream stream = new FileStream(@"C:\Path\To\File\MyXML.xml",FileMode.Open)) 
    persons = (Persons)serializer.Deserialize(stream);
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Thank you for the answer - but is it necessary to define my clase? there is no way to do it anything else? It's big project, I am only do it some method anyway thank you very much for answer! – Elgahir Nov 29 '17 at 21:10
  • Well there can be alternatives but they risk to be tricky and unnecessary complex. Why you need a different approach? – Tommaso Belluzzo Nov 29 '17 at 21:14
  • First of all I don't have list of Person in this class and I don't know at this step how my changes will change it - it's almost end of this project – Elgahir Nov 29 '17 at 21:16