【问题标题】:Search and Update XML using multiple condition using linq使用 linq 使用多个条件搜索和更新 XML
【发布时间】:2020-12-06 02:58:51
【问题描述】:
<Students>
  <Student Name="John">
    <Subject SubId="123" Content="History minor subject" Enrolled="true" Percentage="0"/>
    <Subject SubId="146" Content="Math major Subject"  Enrolled="true" Percentage="0"/>
  </Student>
  <Student Name="Jim">
    <Subject SubId="564" Content="physics medium subject" Enrolled="true" Percentage="0"/>
    <Subject SubId="324" Content="Chem small subject" Enrolled="true" Percentage="0"/>
  </Student>
<Students>

问题 1 - 我想搜索人名内容 John 和内容是否具有“专业”-> 返回 Xelememt ,我正在尝试使用以下查询,但它对我不起作用。

字符串 studentToSearch = "John" 和字符串 contentToSearch = "major"

    IEnumerable<XElement> student = from st in rootElement.Descendants("Students").Elements("Student")
                                             where st.Attribute("Name").Value.ToString() == studentToSearch && st.Element("Subject").Attribute("Content").Value.ToString().Contains(contentToSearch)
                                             select st;
                                                 
                                                 

问题 2 - 我想搜索人名内容 John 和内容是否具有“主要”,如果找到它的百分比为“0”--> 然后将其更新为“80”百分比。尝试以下查询

  rootElement.Elements("Students")
                            .Where(x=> x.Element("Student").Value == studentToSearch)
                            .Where(a => a.Element("Subject").Attributes("Content").ToString().Contains(contentToSearch) && a.Element("Subject").Attribute("Percentage").Value == "0").FirstOrDefault()
                            .SetAttributeValue("Percentage", 80);  

任何建议都会有所帮助?

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    为什么不直接反序列化 xml?您需要使用 SelectMany。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XmlReader reader = XmlReader.Create(FILENAME);
                XmlSerializer serializer = new XmlSerializer(typeof(Students));
                Students students = (Students)serializer.Deserialize(reader);
    
                var johnMajor = students.Student.SelectMany(x => x.Subject.Where(y => y.Content.Contains("major")).Select(z => new { name = x.Name, Subject = z })).FirstOrDefault();
                if (johnMajor.Subject.Percentage == 0) johnMajor.Subject.Percentage = 80;
            }
        }
        public class Students
        {
            [XmlElement()]
            public List<Student> Student { get; set; }
        }
        public class Student
        {
            [XmlAttribute()]
            public string Name { get; set; }
            [XmlElement]
            public List<Subject> Subject { get; set; }
        }
        public class Subject
        {
            [XmlAttribute()]
            public int SubId { get; set; }
            [XmlAttribute()]
            public string Content { get; set; }
            [XmlAttribute()]
            public Boolean Enrolled { get; set; }
            [XmlAttribute()]
            public int Percentage { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多