当前位置:主页 > 软件编程 > .NET代码 >

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

时间:2020-10-31 17:04:27 | 栏目:.NET代码 | 点击:

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

        }

您可能感兴趣的文章:

相关文章