0

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
4

3 に答える 3

1

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;
        }
    }
}
于 2012-10-21T04:27:43.147 に答える
0

linqを使用してこれを行うことができます。ここでのメインマップはidです。これは、idが、例のxmlとリストに基づいてすでに知っているものと同じであるためです。

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

次に、オブジェクトIDが1の場合、リストからすべての値を選択します

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

あなたのxmlクエリはこのようなものかもしれません

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
            };

次のステップはそれをマッピングすることです

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

ID1のオブジェクトでも同じことを行います

于 2012-10-21T04:17:34.847 に答える
0

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.

于 2012-10-21T04:00:13.717 に答える