欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .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;
            }

        }

上一篇:ABP框架中的日志功能完全解析

栏    目:.NET代码

下一篇:c#中datagridview处理非绑定列的方法

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

本文地址:http://www.codeinn.net/misctech/17582.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有