Friday, 15 April 2011

c# - XmlReader back to object list -



c# - XmlReader back to object list -

hey having hard time "rebuild"(read) object. construction simple:

groupname<--object name<--prop addres<--prop phone<--prop

how can rebuild same structure?

here how writing xml

public override void save(phonebook pb) { using (xmlwriter author = xmlwriter.create(path)) { writer.writestartdocument(); writer.writestartelement("group"); foreach (phonebookgroup grouping in pb.items) { writer.writestartelement("groupname"); writer.writeelementstring("groupname", group.groupname.tostring()); foreach (contect contect in group.items) { writer.writestartelement("addres", contect.addres.tostring()); writer.writestartelement("number", contect.number.tostring()); writer.writestartelement("name", contect.name.tostring()); } writer.writeendelement(); } writer.writeendelement(); writer.writeenddocument(); } }

having looked @ xml output changing schema output of save function creates strangely structured xml think reason struggling rebuild it. current output this:

<?xml version="1.0" encoding="utf-8"?> <group> <groupname> <groupname>groupname</groupname> <addres xmlns="addy"> <number xmlns="0123456789"> <name xmlns="henry"> <addres xmlns="address2"> <number xmlns="9876543210"> <name xmlns="dave" /> <groupname> <groupname>secondgroup</groupname> <addres xmlns="fleet"> <number xmlns="0123456789"> <name xmlns="me" /> </number> </addres> </groupname> </number> </addres> </name> </number> </addres> </groupname> </group>

having said resultant xml not unprocessable , can read object using method below. thing have assumed save code there way of getting string groupname object (since using tostring method value xml). in test implemented object implicit conversion operator can see below.

class groupname { public string name { get; set; } public override string tostring() { homecoming this.name; } // specific conversion string groupname object public static implicit operator groupname(string s) { homecoming new groupname() { name = s }; } } public phonebook rebuild() { using (xmlreader reader = xmlreader.create(path)) { // read start of xml reader.movetocontent(); // create homecoming object phonebook returnobject = new phonebook(); returnobject.items = new list<phonebookgroup>(); while (reader.read()) { if (reader.nodetype == xmlnodetype.element) { // read each groupname node separate node collection if (reader.name == "groupname") { // root node of groups phonebookgroup grp = null; contact currentcontact = null; // loop reader starting node while (reader.read()) { if (reader.nodetype == xmlnodetype.element) { switch (reader.name) { case "groupname": if (grp == null) { grp = new phonebookgroup(); returnobject.items.add(grp); // must implement implicit operator between string , groupname object grp.name = (groupname)reader.readelementstring(); } else { // start of new group, null have far , start 1 time again grp = null; } break; case "addres": // address start node contact create new contact , start filling currentcontact = new contact(); if (grp.items == null) { grp.items = new list<contact>(); } grp.items.add(currentcontact); // due way xml beingness written value held namespace !!! currentcontact.address = reader.namespaceuri; break; case "number": // due way xml beingness written value held namespace !!! currentcontact.phone = reader.namespaceuri; break; case "name": // due way xml beingness written value held namespace !!! currentcontact.name = reader.namespaceuri; break; default: break; } } } } } } homecoming returnobject; }

c# xml

No comments:

Post a Comment