简体   繁体   中英

Read XML and map to a List<T> dataset using C#

I have an existing List<MyObject> containing 15 MyObject(s). I want to read an XML document and map the XML document to this data.

The MyObject class has 3 public properties;

+id :int
+value1 :float
+value2 :float

The XML document has this structure;

  <root>

    <objects>
         <object id="1">
             <value1>S</value1>
             <value2>B</value2>
         </object>
         <object id="2">
             <value1>A</value1>
             <value2>J</value2>
         </object>
     </objects>
   </root>

Although the original List<MyObject> has 15 items, the incoming XML document only has 2 items, I need to maps the XML objects by id and change the List values.

so the data for XML document

object id=1, value1 = s, value2= b
object id=2, value1 = a, value2= j

and the data for the List<MyObject> items are

object id=1 value1= a, value2 = b
object id=2 value1= c, value2 = d
object id=3 value1= k, value2 = z
object id=4 value1= y, value2 = e

I need to read the XML document and merge it with the existing List<MyObject> the result of the list should look like;

object id=1 value1= s, value2 = b
object id=2 value1= a, value2 = j
object id=3 value1= k, value2 = z
object id=4 value1= y, value2 = e

Try this

using System.Xml;

...

XmlDocument doc = new XmlDocument();
doc.Load("filename.xml"); //or doc.LoadXml(xmlString);
XmlNodeList objects = doc.GetElementsByTagName("object"); //get list of objects from XML

List<object> myObjects = new List<object> { new object()}; //replace this with your List<object>

for (int i = 0; i < myObjects.Count; i++)
{
    foreach (XmlNode o in objects)
    {
        if (o.Attributes["id"].Value == myObjects[i].id.ToString())
        {
            myObjects[i].Value1 = o.ChildNodes[0].InnerText;
            myObjects[i].Value2 = o.ChildNodes[1].InnerText;
        }
    }
}

To do any kind of search and merge operation, I don't think a List is the most efficient data structure to use since most search operations are going to be of O(N) magnitude. Depending on the size of your dataset, that could be either bad or acceptable.

Secondly, I would override the Equals and GetHashCode methods in order allow for any kind of comparison and identify each object in the set to see if exists or not.

I think the List.Contains method internally invokes those methods in order to compare objects and see if an object exists or not in the list.

Having said that, I think the only approach would be to read the XML file in a separate collection, iterate over it, see if each objects exists in the original collection you have, and replace it if it does.

A LINQ query might simplify the implementation as well.

you can do it with linq your main map here is id since the id is the same you already know based on your example your xml and your list it could be something like this

List<MyObject> myobjectlist = new List<MyObject>();

then i would select all the values from list whe object id is 1

MyObject firstobject=(from c in myobjectlist where c.id=1 selec c).FirstOrDefault();

your xml query might be somethink like this

var query = from node in results.Descendants("objects") 
            where node.Attribute("id").Value == "1"
            select
            {
                value1 = node.Element("value1").Value,
                value2 = node.Element("value2").Value
            };

the next step is to map it

firstobject.value1=query.value1;
firstobject.value2=query.value2;

and do the same with an object that has id 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM