天天看点

xml解析的两种方法

第一种:xml转换成dataset

XmlDataDocument doc=new XmlDataDocument();

TextReader input = new StringReader(xml);

XmlReader reader=new XmlTextReader(input);

doc.DataSet.ReadXml(reader);

reader.Close();

DataTable dtInfo=doc.DataSet.Tables[0];

DataTable dtRelation=doc.DataSet.Tables[1];

DataTable dtAttachment=doc.DataSet.Tables[2];

DataTable dtResult = dtInfo.Copy();

dtResult.Columns.Add("attachment");

foreach (DataRow drResult in dtResult.Rows)

{

DataRow[] drRelations = dtRelation.Select("BaoJuData_Id = " + drResult["BaoJuData_Id"]);

foreach (DataRow drRelation in drRelations)

{

DataRow[] drAttachments = dtAttachment.Select("attachmentlist_Id = " + drRelation["attachmentlist_Id"]);

foreach (DataRow drAttachment in drAttachments)

{

drResult["attachment"] += "<a href="/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" mce_href="/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" "" + drAttachment["url"].ToString().Trim() + "/">"+drAttachment["title"].ToString().Trim() + "</a>"+"<br>";

}

}

}

第二种:xml节点分析

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(xml);

XmlElement root = xmlDoc.DocumentElement;

DataTable dtResult = new DataTable();

if (root.ChildNodes.Count > 0)

{

XmlNode templateNode = root.FirstChild;

foreach (XmlNode node in templateNode.ChildNodes)

{

DataColumn dc = new DataColumn(node.Name);

dtResult.Columns.Add(dc);

}

}

foreach (XmlNode node in root.ChildNodes)

{

DataRow dr = dtResult.NewRow();

for(int i = 0; i < dtResult.Columns.Count; i++)

{

dr[dtResult.Columns[i]] = node.ChildNodes[i].InnerText;

if(dtResult.Columns[i].ColumnName == "id")

dr[dtResult.Columns[i]] = "<a href="/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" mce_href="/" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" "./OAGetshoulidetail.aspx?noid=" +dr[dtResult.Columns[i]] + "/">" + dr[dtResult.Columns[i]] + "</a>";

}

XmlNode nodeAttachmentList = node.SelectSingleNode("//attachmentlist");

dr["attachmentlist"]="";

foreach (XmlNode nodeAtt in nodeAttachmentList.ChildNodes)

{

string title = nodeAtt.SelectSingleNode("//title").InnerText;

string url = nodeAtt.SelectSingleNode("//url").InnerText;

string tag = "<a href="" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" + url + "" mce_href="" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" + url + "">" + title + "</a>";

dr["attachmentlist"] += "<br>" + tag;

}

if (dr["attachmentlist"].ToString() != "")

dr["attachmentlist"] = dr["attachmentlist"].ToString().Substring(4);

dtResult.Rows.Add(dr);