栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1.XML的内容如下:

复制代码 代码如下:


  <BR>    <settings id = "0" name = "显示文字">欢迎您!智慧服务,互动体验......</settings><BR>    <settings id = "1" name = "字体">微软雅黑</settings><BR>    <settings id = "2" name = "颜色">Yellow</settings><BR>    <settings id = "3" name = "字体尺寸">48</settings><BR> 
 


   
   
   
   
 

 
   
     
   

   
     
   

   
     
   

   
     
   

 

 
Picture/main.jpg


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 GetMenuString()
        {
            try
            {
                List list=new List();
                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;
            }

        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126945.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号