欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#操作xml文件示例

程序员文章站 2023-12-19 23:33:10
1. 新增xml文件复制代码 代码如下:xmltoolv2 _xmlhelper = new xmltoolv2(@"c:\20140311blogs.xml");//xm...

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文件

上述代码实现效果:

c#操作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文件

上述代码实现效果:

c#操作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);
});


上述代码实现效果:

c#操作xml文件示例

3.格式化显示xml

复制代码 代码如下:

xmltoolv2 _xmlhelper = new xmltoolv2(@"c:\20140311blogs.xml");//xml保存路径或者读取路径
string _xmlstring = _xmlhelper.showxml();
console.writeline(xmltoolv2.formatxml(_xmlstring, "utf-8"));


上述代码实现效果:

c#操作xml文件示例

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);
                }
            }
        }
    }

上一篇:

下一篇: