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的讀取。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM