方法是这样的
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;
}
我不会写,希望有前辈看到,可以帮帮我!
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;
}
我不会写,希望有前辈看到,可以帮帮我!
解决方案 »
- 你们身边有架构师么?什么是架构师?
- 急 C#DataTable导出成Execl 总是提示object未包含"get_Range"的定义
- 求C#.net下如何实现Tab切换到下一个输入框后能选中覆盖以前输入的内容的方法
- 在BS中gridview 双击一行获取某一行的值,
- @@@@@如何让托盘程序启动就隐藏界面@@@@@
- vs.net的windows 应用程序 要实现在服务器端配置当服务器 在客户端能用 该怎么配置
- 连接Oracle数据库
- 请问如何判断客户端是否支持XSL转换?
- 怎么让程序一开始运行的时侯只在任务栏托盘处显示
- 关于.net bate 2 的MSDN安装的问题
- 请教下 Devexpress控件中 标签和文本框 在一起那个组合控件叫什么
- 怎么重定位Internet Explorer_Server浏览的地址?
代码就不贴拉 自己找找 很多的例子。
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"));
}
{
EdiWaybillEntity ent = new EdiWaybillEntity(); ent.*** = node.Attributes["***"].Value; //取得attribute
ent.*** = node.InnerText; //取得内容
}
然后List<string>对象add()得到一个LIST<STRING>的对象列表集合。
这样不就可以了么 ?