c#操作xml文件示例
1. 新增xml文件
xmltoolv2 _xmlhelper = new xmltoolv2(@"c:\20140311blogs.xml");//xml保存路径或者读取路径
_xmlhelper.create("person", "utf-8");//跟节点名称:person;encode:utf-8
xmlelement _person = _xmlhelper.createelec("name", "yan-zhiwei");//在跟节点后创建person节点
_xmlhelper.setattribute(_person, "gender", "man");//设置person节点属性gender
_xmlhelper.setattribute(_person, "address", "shanghai");//设置person节点属性address
_xmlhelper.save();//保存xml文件
上述代码实现效果:
那在person节点继续增加节点,实现也很简单
_xmlhelper.create("person", "utf-8");//跟节点名称:person;encode:utf-8
xmlelement _person = _xmlhelper.createelec("name", "yan-zhiwei");//在跟节点后创建person节点
_xmlhelper.setattribute(_person, "gender", "man");//设置person节点属性gender
_xmlhelper.setattribute(_person, "address", "shanghai");//设置person节点属性address
xmlelement _worklh = _xmlhelper.createelec(_person, "work", "shanghai lh");//在person节点下增加work节点
_xmlhelper.setattribute(_worklh, "year", "2013~");//设置work节点属性title
xmlelement _workrk = _xmlhelper.createelec(_person, "work", "shanghai ranking");//在person节点下增加work节点
_xmlhelper.setattribute(_workrk, "year", "2011~2013");//设置work节点属性title
_xmlhelper.save();//保存xml文件
上述代码实现效果:
2.读取节点的值,个人感觉xpath方式比linq to xml更为明了方便
xmltoolv2 _xmlhelper = new xmltoolv2(@"c:\20140311blogs.xml");//xml保存路径或者读取路径
xmlnode _person = _xmlhelper.select("person/name/text()");
console.writeline("name:" + _person.innertext);
xmlnode _gender = _xmlhelper.select("person/name[@gender='man']");
console.writeline("gender:" + _gender.attributes["gender"].value);
console.writeline("address:" + _gender.attributes["address"].value);
console.writeline("-----------------------------");
xmlnodelist _workrecord = _xmlhelper.selectnodelist("person/name/work");
xmltoolv2.loop(_workrecord, (xmlnode node) =>
{
console.writeline("work:" + node.innertext + " year:" + node.attributes["year"].value);
});
上述代码实现效果:
3.格式化显示xml
xmltoolv2 _xmlhelper = new xmltoolv2(@"c:\20140311blogs.xml");//xml保存路径或者读取路径
string _xmlstring = _xmlhelper.showxml();
console.writeline(xmltoolv2.formatxml(_xmlstring, "utf-8"));
上述代码实现效果:
4.xmltoolv2源代码
public class xmltoolv2
{
static string _xmlpath;
static xmldocument _xmldoc { get; set; }
static xmlelement _xmlroot;
public xmltoolv2(string xmlpath)
{
_xmlpath = xmlpath;
loadxmldoc();
}
public xmlnode select(string xpath)
{
if (string.isnullorempty(xpath))
throw new argumentnullexception("xpath");
return _xmldoc.selectsinglenode(xpath);
}
public xmlnodelist selectnodelist(string xpath)
{
if (string.isnullorempty(xpath))
throw new argumentnullexception("xpath");
return _xmldoc.selectnodes(xpath);
}
public void create(string rootname, string encode)
{
createxmldoc(rootname, encode);
}
private void createxmldoc(string rootname, string encode)
{
if (!file.exists(_xmlpath))
{
if (string.isnullorempty(rootname))
throw new argumentnullexception(rootname);
_xmldoc = new xmldocument();
xmldeclaration _xmldecl = _xmldoc.createxmldeclaration("1.0", encode, null);
_xmldoc.appendchild(_xmldecl);
_xmlroot = _xmldoc.createelement("", rootname, "");
_xmldoc.appendchild(_xmlroot);
}
}
public void loadxmldoc()
{
if (file.exists(_xmlpath))
{
_xmldoc = new xmldocument();
_xmldoc.load(_xmlpath);
_xmlroot = _xmldoc.documentelement;
}
}
public void save()
{
if (_xmldoc != null)
{
_xmldoc.save(_xmlpath);
}
}
public xmlelement createelec(string elecname, string elecvalue)
{
xmlelement _xelec = createelement(_xmlroot, elecname, elecvalue);
return _xelec;
}
private xmlelement createelement(xmlnode _xmldocselect, string elecname, string elecvalue)
{
if (_xmldocselect != null)
{
xmlelement _xelec = _xmldoc.createelement(elecname);
_xelec.innertext = elecvalue;
_xmldocselect.appendchild(_xelec);
return _xelec;
}
return null;
}
public xmlelement createelec(xmlelement xmlparentelec, string elecname, string elecvalue)
{
if (xmlparentelec != null)
{
xmlelement _xelec = createelement(xmlparentelec, elecname, elecvalue);
return _xelec;
}
return null;
}
public void setattribute(xmlelement xelement, string attrname, string attrvalue)
{
if (xelement != null)
{
xelement.setattribute(attrname, attrvalue);
}
}
public int check(string xpath)
{
if (string.isnullorempty(xpath))
throw new argumentnullexception("xpath");
return selectnodelist(xpath).count;
}
public string showxml()
{
if (_xmldoc != null)
{
return _xmldoc.outerxml;
}
return string.empty;
}
public static string formatxml(string xmlstring, string encode)
{
if (string.isnullorempty(xmlstring))
throw new argumentnullexception("xmlstring");
if (string.isnullorempty(encode))
throw new argumentnullexception("encode");
memorystream _mstream = new memorystream();
xmltextwriter _writer = new xmltextwriter(_mstream, null);
xmldocument _xdoc = new xmldocument();
_writer.formatting = formatting.indented;
_xdoc.loadxml(xmlstring);
_xdoc.writeto(_writer);
_writer.flush();
_writer.close();
encoding _encoding = encoding.getencoding(encode);
string _xmlstring = _encoding.getstring(_mstream.toarray());
_mstream.close();
return _xmlstring;
}
public static void loop(xmlnodelist nodelist, action<xmlnode> xmlnode)
{
if (nodelist != null)
{
foreach (xmlnode xnode in nodelist)
{
xmlnode(xnode);
}
}
}
}