
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad("pathxml"));
XmlNode root = xmlDocDocumentElement;
string backupPath = rootAttributes[0]Value;
string targetPath = rootAttributes[1]Value;
不明白的再问哟,请及时采纳,多谢!
程序如下,其中equal(“”)双引号中的值为节点的属性值
属性值获取如下:
package saxbuilderibmxmlproperty;
import javaioFile;
import javaioIOException;
import javautilIterator;
import orgdom4jAttribute;
import orgdom4jDocument;
import orgdom4jDocumentException;
import orgdom4jElement;
import orgdom4jioSAXReader;
import orgjdomJDOMException;
import orgjdominputSAXBuilder;
public class XmlProperty {
public Element element;
String path = "D:\\xml\\axml";
public void getElementProperty() {
File file = new File(path);
try {
SAXReader reader = new SAXReader();
Document dc = readerread(file);
Element e = dcgetRootElement();
// 节点值
Element child = (Element) egetQName("COMMAND");
Systemoutprintln(childtoString());
// 一级节点
for (Iterator iterator = eelementIterator(); iteratorhasNext();) {
Element el = (Element) iteratornext();
// 一级节点的属性信息
for (Iterator iterator2 = elattributeIterator(); iterator2
hasNext();) {
Attribute attribute = (Attribute) iterator2next();
if(attributegetName()equals("bodyCategory")){
Systemoutprintln("attributegetName()一级节点的属性信息+"+attributegetName()+attributegetValue());
}else{
Systemoutprintln("attributegetName()一级节点的属性信息+"+attributegetName()+attributegetValue());
}
// 二级节点
for (Iterator iterator3 = elelementIterator(); iterator3
hasNext();) {
Element ele = (Element) iterator3next();
// 二级节点的属性信息
for (Iterator iterator4 = eleattributeIterator(); iterator4
hasNext();) {
Attribute attribute1 = (Attribute) iterator4next();
if(attribute1getName()equals("cmdType")){
Systemoutprintln("attribute1getName()二级节点的属性信息+"+attribute1getName()+"+"+attribute1getValue());
}else{
Systemoutprintln("attribute1getName()二级节点的属性信息+"+attribute1getName()+"+"+attribute1getValue());
}
}
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
使用 XPath
//word[@relate="SBV"]|//word[@id= string(/preceding-sibling::[last()]/@parent)]
读取:
public void GetValue(string value,string date)
{
XmlDocument doc = new XmlDocument();
string path = "";
string title = "";
string body = "";
string bl = "";
path = SystemAppDomainCurrentDomainBaseDirectory;//获取文件路径
docLoad(path + "\\dbGuestxml");
XmlNode node = docSelectSingleNode("dbGuest");
XmlNodeList list = nodeChildNodes;
foreach (XmlNode xn in list)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnf1 = xeChildNodes;
foreach (XmlNode xn2 in xnf1)
{
if (xeGetAttribute("days") == value && xeGetAttribute("date") == date)//读取属性
{
if (xn2Name == "title")//获得标题
{
title = xn2InnerText;
}
if (xn2Name == "body")//获得内容
{
body = xn2InnerText;
}
if (xn2Name == "bl")
{
bl = xn2InnerText;
}
}
}
}
lblmesText = title;
txtbodyText = body;
txtwcdText = bl;
}
保存:
private void btnComfrim_Click(object sender, EventArgs e)
{
string path = "";
// string title = "";
if (txtwcdText == "")
{
lblshowmesText = "任务完成度不能为空";
txtwcdFocus();
lblshowmesText = "";
}
XmlDocument doc = new XmlDocument();
try
{
path = SystemAppDomainCurrentDomainBaseDirectory;//获取文件路径
docLoad(path + "\\dbGuestxml");
XmlNode node = docSelectSingleNode("dbGuest");
XmlNodeList list = nodeChildNodes;
foreach (XmlNode xn in list)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnf1 = xeChildNodes;
foreach (XmlNode xn2 in xnf1)
{
if (xeGetAttribute("days") == valueToString())//读取属性
{
if (xn2Name == "body")//获得内容
{
xn2InnerText = txtbodyText;
}
else if (xn2Name == "bl")
{
xn2InnerText = txtwcdText;
}
if (xn2Name == "title")
{
xn2InnerText = lblmesText;
}
xeSetAttribute("date",DateTimeNowToString("yyyy-MM"));
}
}
}
docSave(path + "\\dbGuestxml");
lblshowmesText = "提示:任务已成功提交!";
}
catch (Exception ex)
{
}
}
根据你的情况去修改下代码吧,这是我之前用的,可对节点进行修改。
以上就是关于winfrom 读取xml元素节点全部的内容,包括:winfrom 读取xml元素节点、java语句如何获取XML文件的节点值、xml 寻找xml节点集等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)