欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#通過XML節(jié)點(diǎn)屬性/屬性值讀取寫入XML操作代碼實(shí)例

 更新時(shí)間:2013年11月27日 15:04:24   作者:  
本文主要介紹C#通過XML節(jié)點(diǎn)屬性、屬性值對(duì)XML的讀取,寫入操作,大家參考使用吧

1.XML的內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <title>
    <settings id = "0" name = "顯示文字">歡迎您!智慧服務(wù),互動(dòng)體驗(yàn)......</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="會(huì)議安排"/>
  </menu>
  <mu1>
    <submu id = "0" name = "iCentroView產(chǎn)品">
      <video id = "0">Videos/ICV.mp4</video>
    </submu>
    <submu id = "1" name = "員工社區(qū)">
      <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文檔

復(fù)制代碼 代碼如下:

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節(jié)點(diǎn)中的內(nèi)容

復(fù)制代碼 代碼如下:

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中節(jié)點(diǎn)的屬性值

復(fù)制代碼 代碼如下:

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.通過節(jié)點(diǎn)獲取XML節(jié)點(diǎn)中的內(nèi)容

復(fù)制代碼 代碼如下:

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;
            }

        }

相關(guān)文章

最新評(píng)論