xml文档读取

作者:袖梨 2022-06-25

xml文档读取

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.xml;

namespace operatexml
{
    class program
    {
        static void main(string[] args)
        {
            try
            {
                //xml文件存储路径
                string myxmlfilepath = "e:mycomputers.xml";
                //生成xml文件
                generatexmlfile(myxmlfilepath);
            }
            catch (exception ex)
            {
                console.writeline(ex.tostring());
            }
        }

        private static void generatexmlfile(string xmlfilepath)
        {
            try
            {
                //初始化一个xml实例
                xmldocument myxmldoc = new xmldocument();
                //创建xml的根节点
                xmlelement rootelement = myxmldoc.createelement("computers");
                //将根节点加入到xml文件中(appendchild)
                myxmldoc.appendchild(rootelement);

                //初始化第一层的第一个子节点
                xmlelement firstlevelelement1 = myxmldoc.createelement("computer");
                //填充第一层的第一个子节点的属性值(setattribute)
                firstlevelelement1.setattribute("id", "11111111");
                firstlevelelement1.setattribute("description", "made in china");
                //将第一层的第一个子节点加入到根节点下
                rootelement.appendchild(firstlevelelement1);
                //初始化第二层的第一个子节点
                xmlelement secondlevelelement11 = myxmldoc.createelement("name");
                //填充第二层的第一个子节点的值(innertext)
                secondlevelelement11.innertext = "lenovo";
                firstlevelelement1.appendchild(secondlevelelement11);
                xmlelement secondlevelelement12 = myxmldoc.createelement("price");
                secondlevelelement12.innertext = "5000";
                firstlevelelement1.appendchild(secondlevelelement12);


                xmlelement firstlevelelement2 = myxmldoc.createelement("computer");
                firstlevelelement2.setattribute("id", "2222222");
                firstlevelelement2.setattribute("description", "made in usa");
                rootelement.appendchild(firstlevelelement2);
                xmlelement secondlevelelement21 = myxmldoc.createelement("name");
                secondlevelelement21.innertext = "ibm";
                firstlevelelement2.appendchild(secondlevelelement21);
                xmlelement secondlevelelement22 = myxmldoc.createelement("price");
                secondlevelelement22.innertext = "10000";
                firstlevelelement2.appendchild(secondlevelelement22);

                //将xml文件保存到指定的路径下
                myxmldoc.save(xmlfilepath);
            }
            catch (exception ex)
            {
                console.writeline(ex.tostring());
            }
        }
    }
}

这里要强调一点:xmlelement是xmlnode的继承,所以xmlelement可以调用更加多的方法,实现相应的功能。

 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.xml;

namespace operatexml
{
    class program
    {
        static void main(string[] args)
        {
            try
            {
                //xml文件存储路径
                string myxmlfilepath = "e:mycomputers.xml";
                //修改xml文件的节点信息
                modifyxmlinformation(myxmlfilepath);
            }
            catch (exception ex)
            {
                console.writeline(ex.tostring());
            }
        }

        private static void modifyxmlinformation(string xmlfilepath)
        {
            try
            {
                xmldocument myxmldoc = new xmldocument();
                myxmldoc.load(xmlfilepath);
                xmlnode rootnode = myxmldoc.firstchild;
                xmlnodelist firstlevelnodelist = rootnode.childnodes;
                foreach (xmlnode node in firstlevelnodelist)
                {
                    //修改此节点的属性值
                    if (node.attributes["description"].value.equals("made in usa", stringcomparison.invariantcultureignorecase))
                    {
                        node.attributes["description"].value = "made in hongkong";
                    }
                }
                //要想使对xml文件所做的修改生效,必须执行以下save方法
                myxmldoc.save(xmlfilepath);
            }
            catch (exception ex)
            {
                console.writeline(ex.tostring());
            }

        }

    }
}

 

xml文档


 
    lenovo
    5000
 

 
    ibm
    10000
 

相关文章

精彩推荐