日期:2014-05-18  浏览次数:21111 次

C# 如何把将xml文档转换成列表集合,在线等!
方法是这样的
public IList<EdiWaybillEntity> GetListByXML(XmlDocument doc)
{
 if (doc.ChildNodes.Count != 1)
  return null;
  if (doc.ChildNodes[0].Name != typeof(EdiWaybillEntity).Name + "s")
  return null;
  //XmlNode node = doc.ChildNodes[0];
  IList<EdiWaybillEntity> WBentList = new List<EdiWaybillEntity>();
  //读取根节点的所有子节点,放到nodeList中
  XmlNodeList nodeList = doc.SelectSingleNode("EDIWaybillEntityImport").ChildNodes;
  //查找二级节点的内容或属性
  //foreach (XmlElement root in nodeList)
  foreach(XmlNode node in nodeList)
  {
  EdiWaybillEntity ent = new EdiWaybillEntity();
  }

   
   
  return WBentList;
}
我不会写,希望有前辈看到,可以帮帮我!

------解决方案--------------------
XML 可以反序列化为List
代码就不贴拉 自己找找 很多的例子。
------解决方案--------------------
C# code

public class OrderGoodInfo
    {
        private int _type;
        private int _goodid;
        private int _number;
        private string _name;
        private decimal _price;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
       

        public int Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public int GoodId
        {
            get { return _goodid; }
            set { _goodid = value; }
        }

        public int Number
        {
            get { return _number; }
            set { _number = value; }
        }

        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }
    }

#region 实体类,XML互操作
        /// <summary>
        /// 实体类序列化成xml
        /// </summary>
        /// <param name="enitities">The enitities.</param>
        /// <param name="headtag">The headtag.</param>
        /// <returns></returns>
        public static string ObjListToXml<T>(List<T> enitities, string headtag)
        {
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] propinfos = null;
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<"+headtag+">");
            foreach (T obj in enitities)
            {
                //初始化propertyinfo
                if (propinfos == null)
                {
                    Type objtype = obj.GetType();
                    propinfos = objtype.GetProperties();
                }

                sb.AppendLine("<item>");
                foreach (PropertyInfo propinfo in propinfos)
                {
                    sb.Append("<");
                    sb.Append(propinfo.Name);
                    sb.Append(">");
                    sb.Append(propinfo.GetValue(obj, null));
                    sb.Append("</");
                    sb.Append(propinfo.Name);
                    sb.AppendLine(">");
                }
                sb.AppendLine("</item>");
            }
            sb.AppendLine("</" + headtag + ">");
            return sb.ToString();
        }

        /// <summary>
        /// 使用XML初始化实体类容器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="typename">The typename.</param>
        /// <param name="xml&quo