xml linq 读写操作

 

  public class LotteryInfo
    {
        /// <summary>
        /// 根据奖名称,获取信息,如:一等奖
        /// </summary>
        /// <param name="Name">奖名称</param>
        /// <returns></returns>
        public static LotteryInfo LotteryInfoInit(string Name)
        {
            XDocument xdoc = XDocument.Load(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
            LotteryInfo info = xdoc.Descendants("Main").Where(s => s.Attribute("name").Value == Name).Select(w => new LotteryInfo()
                {
                    Name = w.Descendants("Name").Select(s => s.Value).FirstOrDefault(),
                    Value = w.Descendants("Value").Select(s => s.Value).FirstOrDefault(),
                    PrizeName = w.Descendants("PrizeName").Select(s => s.Value).FirstOrDefault(),
                    IsSpecified = w.Descendants("IsSpecified").Select(s => s.Value).FirstOrDefault(),
                    WinningName = w.Descendants("WinningName").Select(s => s.Value).FirstOrDefault(),
                    Order = w.Descendants("Order").Select(s => s.Value).FirstOrDefault(),
                }).FirstOrDefault();
            return info;            
        }

        /// <summary>
        /// 将XML数据读取到Datatable
        /// </summary>
        /// <returns></returns>
        public static IList<LotteryInfo> GetList()
        {
            XDocument xdoc = XDocument.Load(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
            IList<LotteryInfo> info = xdoc.Descendants("Main").Select(w => new LotteryInfo()
            {
                Name = w.Descendants("Name").Select(s => s.Value).FirstOrDefault(),
                Value = w.Descendants("Value").Select(s => s.Value).FirstOrDefault(),
                PrizeName = w.Descendants("PrizeName").Select(s => s.Value).FirstOrDefault(),
                IsSpecified = w.Descendants("IsSpecified").Select(s => s.Value).FirstOrDefault(),
                WinningName = w.Descendants("WinningName").Select(s => s.Value).FirstOrDefault(),
                Order = w.Descendants("Order").Select(s => s.Value).FirstOrDefault(),
            }).ToList();
            return info;  
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="Name">奖名称</param>
        /// <param name="NodeName">节点名称</param>
        /// <param name="value"></param>
        public static void SetValue(string Name, string NodeName, string value)
        {
            XDocument xdoc = XDocument.Load(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
            xdoc.Descendants("Main").Where(s => s.Attribute("name").Value == Name).Descendants(NodeName).FirstOrDefault().SetValue(value);
            xdoc.Save(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
        }

        /// <summary>
        /// 添加结点
        /// </summary>
        /// <param name="info"></param>
        public static void AddElement(LotteryInfo info)
        {
            XDocument xdoc = XDocument.Load(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
            XElement xel = new XElement("Main", "", new XAttribute("name", info.Name));

            XElement xel1 = new XElement("Name", info.Name);
            XElement xel2 = new XElement("Value", info.Value);
            XElement xel3 = new XElement("PrizeName", info.PrizeName);
            XElement xel4 = new XElement("IsSpecified", info.IsSpecified);
            XElement xel5 = new XElement("WinningName", info.WinningName);
            XElement xel6 = new XElement("Order", info.Order);

            xel.Add(xel1);
            xel.Add(xel2);
            xel.Add(xel3);
            xel.Add(xel4);
            xel.Add(xel5);
            xel.Add(xel6);

            xdoc.Element("Lottery").Add(xel);
            xdoc.Save(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
        }

        /// <summary>
        /// 删除结点
        /// </summary>
        /// <param name="Name">奖名称</param>
        public static void DelElement(string Name)
        {
            try
            {
                XDocument xdoc = XDocument.Load(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
                xdoc.Descendants("Main").Where(s => s.Attribute("name").Value == Name).FirstOrDefault().Remove();
                xdoc.Save(Application.StartupPath + @"\LotteryConfig\Lottery.xml");
            }
            catch { return; }
        }

        public string Name { set; get; }
        public string Value { set; get; }
        public string PrizeName { set; get; }
        public string IsSpecified { set; get; }
        public string WinningName { set; get; }
        public string Order { set; get; }
    }



<?xml version="1.0" encoding="utf-8"?>
<Lottery>
  <Main name="一等奖">
    <Name>一等奖</Name>
    <Value>50</Value>
    <PrizeName>奇瑞QQ</PrizeName>
    <IsSpecified>0</IsSpecified>
    <WinningName>A003.jpg</WinningName>
    <Order>0</Order>
  </Main>
  <Main name="二等奖">
    <Name>二等奖</Name>
    <Value>4</Value>
    <PrizeName>切糕</PrizeName>
    <IsSpecified>0</IsSpecified>
    <WinningName></WinningName>
    <Order>1</Order>
  </Main>
  <Main name="三等奖">
    <Name>三等奖</Name>
    <Value>6</Value>
    <PrizeName>电车</PrizeName>
    <IsSpecified>1</IsSpecified>
    <WinningName>A004.jpg</WinningName>
    <Order>2</Order>
  </Main>
</Lottery>  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在C#中,使用Linq to XML可以方便地进行XML文件的读写操作。首先,我们需要加载XML文件,可以使用XDocument类的Load方法来加载文件\[1\]。然后,我们可以通过获取根元素来访问XML文件的内容,使用Root属性可以获取根元素\[2\]。接下来,我们可以使用Elements方法获取根元素的所有直接子元素,并通过遍历这些子元素来获取它们的名称和属性值\[2\]。如果需要进一步访问子元素的子元素,可以使用Elements方法继续获取\[2\]。另外,如果XML文件中使用了命名空间,我们可以使用XName类的Get方法来指定命名空间\[3\]。通过这些方法,我们可以方便地读取和解析XML文件中的数据。当需要写入XML文件时,可以使用XDocument类的Save方法将修改后的XML文件保存到指定的路径。 #### 引用[.reference_title] - *1* *3* [Linq to Xml读写xml](https://blog.csdn.net/donghan2019/article/details/101946892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#基础精华06(Linq To XML,读取xml文件,写入xml)](https://blog.csdn.net/XHQT520/article/details/50401669)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值