源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-03-23 13:10 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#通过XML节点属性/属性值读取写入XML操作代码实例
[b]1.XML的内容如下:[/b]
[u]复制代码[/u] 代码如下:
<?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>
[b]2.获得XML文档[/b]
[u]复制代码[/u] 代码如下:
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;             }         }
[b]3.通过属性名称读取XML节点中的内容[/b]
[u]复制代码[/u] 代码如下:
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;             }         }
[b]4.通过属性读取XML中节点的属性值[/b]
[u]复制代码[/u] 代码如下:
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;             }         }
[b]5.通过节点获取XML节点中的内容[/b]
[u]复制代码[/u] 代码如下:
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;             }         }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部