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

C#通过XML节点属性/属性值读取写入XML操作代码实例

程序员文章站 2024-02-22 10:26:22
1.xml的内容如下: 复制代码 代码如下: ...

1.xml的内容如下:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <title>
    <settings id = "0" name = "显示文字">欢迎您!智慧服务,互动体验......</settings>
    <settings id = "1" name = "字体">微软雅黑</settings>
    <settings id = "2" name = "颜色">yellow</settings>
    <settings id = "3" name = "字体尺寸">48</settings>
  </title>
  <menu>
    <submu id="0" name="部门分布"/>
    <submu id="1" name="宣传视频"/>
    <submu id="2" name="部门宣传"/>
    <submu id="3" name="会议安排"/>
  </menu>
  <mu1>
    <submu id = "0" name = "icentroview产品">
      <video id = "0">videos/icv.mp4</video>
    </submu>
    <submu id = "1" name = "员工社区">
      <video id = "0">videos/ygsqxcp.mp4</video>
    </submu>
    <submu id = "2" name = "三维展示">
      <video id = "0">videos/ibaosight.mp4</video>
    </submu>
    <submu id = "1" name = "好生活宣传">
      <video id = "0">videos/goodlift.mp4</video>
    </submu>
  </mu1>
  <main>picture/main.jpg</main>
</root>

2.获得xml文档

复制代码 代码如下:

private static string url = appdomain.currentdomain.setupinformation.applicationbase+"config\\config.xml";
        private  xmldocument xmldoc;
        private xmlnode root;
        public static string title;
        public  xmlhandler()
        {
            xmldoc = new xmldocument();
            loadconfig();
        }

        private  void loadconfig()
        {
            try
            {
                xmldoc.load(url);
                root = xmldoc.selectsinglenode("root");
            }
            catch (exception e)
            {
                throw e;
            }
        }

3.通过属性名称读取xml节点中的内容

复制代码 代码如下:

public titlemodel gettitle()
        {
            try
            {
                titlemodel title = new titlemodel();
                xmlnode node = root.firstchild;
                if(node!=null)
                {
                    foreach (xmlnode nd in node.childnodes)
                    {
                        xmlelement element = (xmlelement)nd;
                        if (element.getattribute("name") == "显示文字")
                        {
                            title.title = nd.innertext;
                        }
                        else if (element.getattribute("name") == "字体尺寸")
                        {
                            title.fontsize = convert.toint32(nd.innertext);
                        }
                        else if (element.getattribute("name") == "颜色")
                        {
                            title.fontcolor = fontcolor(nd.innertext);
                        }
                        else if (element.getattribute("name") == "字体")
                        {
                            title.fontfamily = nd.innertext;
                        }
                    }
                }
                return title;       
            }
            catch (exception e)
            {       
                throw e;
            }

        }

4.通过属性读取xml中节点的属性值

复制代码 代码如下:

public list<string> getmenustring()
        {
            try
            {
                list<string> list=new list<string>();
                xmlnode menu = root.childnodes[1];
                if (menu != null)
                {
                    foreach (xmlnode node in menu.childnodes)
                    {
                        xmlelement element = (xmlelement)node;
                        list.add(element.getattribute("name"));
                    }
                }
                return list;
            }
            catch (exception e)
            {
                throw e;
            }
        }

5.通过节点获取xml节点中的内容

复制代码 代码如下:

public string getmainbackground()
        {
            string url ="images/mainjpg";
            try
            {
                xmlnode node = root.lastchild;
                if (node != null)
                {
                    url =  node.innertext;
                }
                return url;
            }
            catch (exception e)
            {
                throw e;
            }

        }