• C#中XML的读取


      本文主要介绍在C#中有关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;
                }
     
            }

      以上就完成,通过节点属性、属性值对XML的读取。

  • 相关阅读:
    winform制作自定义控件(入门)
    VB2012读取xml
    VB生成xml
    通宵疯狂积累VB.NET基础知识
    【转】vs2010下创建webservice
    React Native 快速入门之认识Props和State
    mac下 mysql / nginx 问题总汇
    Oracle例外定义
    mac os x 把reids nignx mongodb做成随机启动吧
    Oracle 记录
  • 原文地址:https://www.cnblogs.com/akwwl/p/3393736.html
Copyright © 2020-2023  润新知