//
在NET中处理XML数据是很方便的,但是对于初学者来说,可能有时候还是有些困难,
// 以下代码是将表转换成XmlDocument,然后在读取出来的示例,很简单,可以自己在进行扩充.
private XmlDocument CreateXmlDocument()
... {
XmlDocument xmldoc = new XmlDocument();
XmlNode xnod;
XmlElement xenl;
//描述信息
xnod = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xnod.InnerText += " encoding="UTF-8"";
xmldoc.AppendChild(xnod);
//根元素
xenl = xmldoc.CreateElement("", "body", "");
xmldoc.AppendChild(xenl);
XmlNode nod = xmldoc.SelectSingleNode("body");
//设置元素的值
XmlElement element;
element = xmldoc.CreateElement("item");
element.SetAttribute("Name","Manqing Yu");
element.SetAttribute("Age","24");
element.SetAttribute("Sex", "男");
nod.AppendChild(element);
}
xmldoc.Save( " c:XmlTest.xml " );
return xmldoc;
}
// 读取XML数据
XmlNodeList nodeList = xmldoc.SelectNodes( " body/item " );
StringBuilder strBody;
for ( int row = 0 ; row < nodeList.Count; row ++ )
... {
MessageBox.Show(nodeList[row].Attributes["Name"].Value.ToString());
MessageBox.Show(nodeList[row].Attributes["Age"].Value.ToString());
MessageBox.Show(nodeList[row].Attributes["Sex"].Value.ToString());
}
// 以下代码是将表转换成XmlDocument,然后在读取出来的示例,很简单,可以自己在进行扩充.
private XmlDocument CreateXmlDocument()
... {
XmlDocument xmldoc = new XmlDocument();
XmlNode xnod;
XmlElement xenl;
//描述信息
xnod = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xnod.InnerText += " encoding="UTF-8"";
xmldoc.AppendChild(xnod);
//根元素
xenl = xmldoc.CreateElement("", "body", "");
xmldoc.AppendChild(xenl);
XmlNode nod = xmldoc.SelectSingleNode("body");
//设置元素的值
XmlElement element;
element = xmldoc.CreateElement("item");
element.SetAttribute("Name","Manqing Yu");
element.SetAttribute("Age","24");
element.SetAttribute("Sex", "男");
nod.AppendChild(element);
}
xmldoc.Save( " c:XmlTest.xml " );
return xmldoc;
}
// 读取XML数据
XmlNodeList nodeList = xmldoc.SelectNodes( " body/item " );
StringBuilder strBody;
for ( int row = 0 ; row < nodeList.Count; row ++ )
... {
MessageBox.Show(nodeList[row].Attributes["Name"].Value.ToString());
MessageBox.Show(nodeList[row].Attributes["Age"].Value.ToString());
MessageBox.Show(nodeList[row].Attributes["Sex"].Value.ToString());
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1534744