
C# *** 作XML
有以下几种方式:
1:使用XmlDocument相关类库和方法 *** 作xml
2:使用XDocument相关类库和方法 *** 作xml
3:使用XmlReader和XmlWriter相关类库和方法 *** 作xml
获得指定节点的值也需要
分为属性和元素
1:使用XmlDocument
XmlDocument
doc
=
new
XmlDocument();
docLoad("Customer2xml");
//
XmlNodeList
nodeList
=
docGetElementsByTagName("row");
XmlNodeList
nodeList
=
docSelectNodes("/Table/row");
//读取属性+元素
foreach
(XmlNode
item
in
nodeList)
{
customerInfoAppId
=
item
Attributes
["AppID"]Value;
customerInfoCustomerID
=
item["CustomerID"]InnerText;
}
2:使用XDocument
XDocument
xdoc
=
XDocumentLoad("Customer2xml");
var
custs
=
from
customer
in
xdocDescendants("row")
select
new
{//读取属性+元素
Version
=
customerAttribute("Version")Value,
CustomerID
=
customerElement("CustomerID")Value,
};
3:使用XmlReader
XmlReader
reader
=
XmlReaderCreate("Customer2xml",
settings);
//读取属性
while
(readerRead())
{
if
(readerNodeType
==
XmlNodeTypeElement)
{
switch
(readerName)
{
case
"row":
customerInfo
=
new
CustomerInfo();
if
(readerHasAttributes)
{
customerInfoAppId
=
readerGetAttribute("AppID");
customerInfoVersion
=
readerGetAttribute("Version");
}
break;
case
"CustomerID":
customerInfoCustomerID
=
readerReadString();
break;
}
}
dom4j中,使用Elementattributes方法可以获取到节点的属性,而使用elements则可以获取相应的子节点
比如:
Element root = docgetRootElement();
List attrList = rootattributes();
for (int i = 0; i < attrListsize(); i++) {
//属性的取得
Attribute item = (Attribute)attrListget(i);
Systemoutprintln(itemgetName() + "=" + itemgetValue());
}
List childList = rootelements();
for (int i = 0; i < childListsize(); i++) {
//子节点的 *** 作
Element it = (Element) childListget(i);
//对子节点进行其它 *** 作
}
以上就是关于C# *** 作XML,如何获取指定节点值全部的内容,包括:C# *** 作XML,如何获取指定节点值、如何用DOM4J读取XML文件中的元素值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)