zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

2023-06-13 09:15:13 时间

1.XML的内容如下:

复制代码代码如下:


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

2.获得XML文档

复制代码代码如下:


privatestaticstringurl=AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Config\\config.xml";
       private XmlDocumentxmlDoc;
       privateXmlNoderoot;
       publicstaticstringTitle;
       public XMLHandler()
       {
           xmlDoc=newXmlDocument();
           LoadConfig();
       }

       private voidLoadConfig()
       {
           try
           {
               xmlDoc.Load(url);
               root=xmlDoc.SelectSingleNode("root");
           }
           catch(Exceptione)
           {
               throwe;
           }
       }

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

复制代码代码如下:
publicTitleModelGetTitle()
       {
           try
           {
               TitleModeltitle=newTitleModel();
               XmlNodenode=root.FirstChild;
               if(node!=null)
               {
                   foreach(XmlNodendinnode.ChildNodes)
                   {
                       XmlElementelement=(XmlElement)nd;
                       if(element.GetAttribute("name")=="显示文字")
                       {
                           title.Title=nd.InnerText;
                       }
                       elseif(element.GetAttribute("name")=="字体尺寸")
                       {
                           title.FontSize=Convert.ToInt32(nd.InnerText);
                       }
                       elseif(element.GetAttribute("name")=="颜色")
                       {
                           title.FontColor=FontColor(nd.InnerText);
                       }
                       elseif(element.GetAttribute("name")=="字体")
                       {
                           title.FontFamily=nd.InnerText;
                       }
                   }
               }
               returntitle;       
           }
           catch(Exceptione)
           {       
               throwe;
           }

       }

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

复制代码代码如下:
publicList<string>GetMenuString()
       {
           try
           {
               List<string>list=newList<string>();
               XmlNodemenu=root.ChildNodes[1];
               if(menu!=null)
               {
                   foreach(XmlNodenodeinmenu.ChildNodes)
                   {
                       XmlElementelement=(XmlElement)node;
                       list.Add(element.GetAttribute("name"));
                   }
               }
               returnlist;
           }
           catch(Exceptione)
           {
               throwe;
           }
       }

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

复制代码代码如下:
publicstringGetMainBackground()
       {
           stringurl="Images/mainjpg";
           try
           {
               XmlNodenode=root.LastChild;
               if(node!=null)
               {
                   url= node.InnerText;
               }
               returnurl;
           }
           catch(Exceptione)
           {
               throwe;
           }

       }