首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 怎么把将xml文档转换成列表集合,

2012-09-06 
C# 如何把将xml文档转换成列表集合,在线等!方法是这样的public IListEdiWaybillEntity GetListByXML(Xml

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">The XML.</param>        /// <param name="headtag">The headtag.</param>        /// <returns></returns>        public static List<T> XmlToObjList<T>(string xml,string headtag)             where T : new()        {            List<T> list = new List<T>();            XmlDocument doc = new XmlDocument();            PropertyInfo[] propinfos = null;            doc.LoadXml(xml);            XmlNodeList nodelist = doc.SelectNodes("/" + headtag + "/item");            foreach (XmlNode node in nodelist)            {                T entity = new T();                //初始化propertyinfo                if (propinfos == null)                {                    Type objtype = entity.GetType();                    propinfos = objtype.GetProperties();                }                //填充entity类的属性                foreach (PropertyInfo propinfo in propinfos)                {                    XmlNode cnode = node.SelectSingleNode(propinfo.Name);                    string v = cnode.InnerText;                    if (v != null)                         propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);                }                list.Add(entity);            }            return list;        }        #endregion    }使用如下:protected void Page_Load(object sender, EventArgs e)    {        OrderGoodInfo good1 = new OrderGoodInfo();        good1.GoodId = 1;        good1.Number = 1;        good1.Price = 100;        good1.Type = 1;        OrderGoodInfo good2= new OrderGoodInfo();        good2.GoodId = 2;        good2.Number = 2;        good2.Price = 200;        good2.Type = 2;        List<OrderGoodInfo> list = new List<OrderGoodInfo>();        list.Add(good1);        list.Add(good2);                string xmlstr = XmlHelper.ObjListToXml(list, "goods");        Response.Write(xmlstr);        List<OrderGoodInfo> nlist = XmlHelper.XmlToObjList<OrderGoodInfo>(xmlstr, "goods");        Response.Write(XmlHelper.ObjListToXml(nlist, "goods"));    } 


[解决办法]
你这不是已经写的挺好的了吗

foreach(XmlNode node in nodeList)
{
EdiWaybillEntity ent = new EdiWaybillEntity();

ent.*** = node.Attributes["***"].Value; //取得attribute
ent.*** = node.InnerText; //取得内容
}
[解决办法]
foreach(XmlNode node in nodeList) 
然后List<string>对象add()得到一个LIST<STRING>的对象列表集合。 
这样不就可以了么 ?

热点排行