xml linq 读写操作

本文介绍了一个用于管理和配置彩票信息的类库。该库提供了初始化彩票信息、读取XML配置文件到数据表、更新、添加和删除彩票信息的方法。通过这些方法可以方便地维护彩票的各项属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  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>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值