Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到xml获取列表列表_C#_List_Linq To Xml - Fatal编程技术网

C# Linq到xml获取列表列表

C# Linq到xml获取列表列表,c#,list,linq-to-xml,C#,List,Linq To Xml,我有一个XML <Treatments> <Treatment> <Date>...</Date> <Photos> <Photo> <Path>...<Path> <Contour>

我有一个XML

    <Treatments>
        <Treatment>
            <Date>...</Date>
            <Photos>
               <Photo>
                  <Path>...<Path>
                  <Contour>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                  <Contour>
               </Photo>
               <Photo>
                  <Path>...<Path>
                  <Contour>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                      <Line X1 = ... Y1 = ... X2 = ... Y2 = ...>
                  <Contour>
               </Photo>
            </Photos>
         </Treatment>
         <Treatment>
            .
            .
         </Treatment
    </Treatments>

...
...
...
.
.
c、 ToList();
tl.Add(td);
}
我不知道如何将一行列表添加到每张照片列表中。
我是linq新手,有谁能告诉我正确的方法吗?

我更愿意使用Xml反序列化,但使用Linq2XML时,它看起来是这样的(使用unittest来显示/证明功能):

[TestClass]
公共类UnitTest1
{
[测试方法]
公共void TestMethod1()
{
字符串xml=@“
2015-04-13
...
";
XElement=XElement.Parse(xml);
列表测试=xelement.Elements(“处理”)
.选择(处理节点=>新建处理数据
{
Date=DateTime.ParseExact(treatmentNode.Element(“Date”).Value,“yyyy-MM-dd”,CultureInfo.InvariantCulture),
照片=治疗节点。元素(“照片”)。元素(“照片”)
.选择(photoNode=>新建照片
{
Path=photoNode.Element(“Path”).Value,
线=光节点。元素(“轮廓”)。元素(“线”)
.选择(lineNode=>新行
{
X1=int.Parse(lineNode.Attribute(“X1”).Value),
Y1=int.Parse(lineNode.Attribute(“Y1”).Value),
X2=int.Parse(lineNode.Attribute(“X2”).Value),
Y2=int.Parse(lineNode.Attribute(“Y2”).Value),
}).ToArray()
}).ToArray()
}).ToList();
Assert.AreEqual(155,test.First().Photos.First().Lines.First().X2);
}
}
公共类处理数据
{
公共日期时间日期{get;set;}
公共IEnumerable照片{get;set;}
}
公开课照片
{
公共字符串路径{get;set;}
公共IEnumerable行{get;set;}
}
公共班级线
{
公共int X1{get;set;}
公共整数Y1{get;set;}
公共int X2{get;set;}
公共int Y2{get;set;}
}

您能介绍一下
治疗数据的定义,并提供一些您希望得到的结果的详细信息吗?这是Florian写的。只是我使用的是列表而不是IEnumerable。我只是准备发布一些几乎相同的东西;)请注意,只需使用
子代(“照片”)
子代(“行”)
即可节省一些输入。
IEnumerable<XElement> treats =
        from t in xdoc.Descendants("Treatment")
        where (string)t.Element("Date").Value == _Date
        select t;

List<TreatmentData> tl = new List<TreatmentData>();

foreach (XElement treat in treats)
{
   TreatmentData td = new TreatmentData();
   td.Date = treat.Element("Date").Value;
   // td.Photos is a List<string>
   td.Photos = treat.Element("Photos").Elements("Path").Select(c => c.Value).ToList();
   tl.Add(td);
}
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        string xml = @"
<Treatments>
    <Treatment>
        <Date>2015-04-13</Date>
        <Photos>
           <Photo>
              <Path>...</Path>
              <Contour>
                  <Line X1 = ""1"" Y1 = ""1"" X2 = ""155"" Y2 = ""1""/>
                  <Line X1 = ""133"" Y1 = ""1"" X2 = ""122"" Y2 = ""1""/>
              </Contour>
           </Photo>              
         </Photos>
     </Treatment>
</Treatments>";
        XElement xelement = XElement.Parse(xml);

        List<TreatmentData> test = xelement.Elements("Treatment")
            .Select(treatmentNode => new TreatmentData
                                     {
                                         Date = DateTime.ParseExact(treatmentNode.Element("Date").Value, "yyyy-MM-dd", CultureInfo.InvariantCulture),
                                         Photos = treatmentNode.Element("Photos").Elements("Photo")
                                            .Select(photoNode => new Photo
                                                                {
                                                                   Path = photoNode.Element("Path").Value,
                                                                   Lines = photoNode.Element("Contour").Elements("Line")
                                                                   .Select(lineNode => new Line
                                                                                        {
                                                                                            X1 = int.Parse(lineNode.Attribute("X1").Value),
                                                                                            Y1 = int.Parse(lineNode.Attribute("Y1").Value),
                                                                                            X2 = int.Parse(lineNode.Attribute("X2").Value),
                                                                                            Y2 = int.Parse(lineNode.Attribute("Y2").Value),
                                                                            }).ToArray()
                                                       }).ToArray()
                                     }).ToList();

        Assert.AreEqual(155,test.First().Photos.First().Lines.First().X2);


    }
}
public class TreatmentData
{
    public DateTime Date { get; set; }
    public IEnumerable<Photo> Photos { get; set; }

}

public class Photo
{
    public string Path { get; set; }
    public IEnumerable<Line> Lines { get; set; }

}

public class Line
{
    public int X1 { get; set; }
    public int Y1 { get; set; }
    public int X2 { get; set; }
    public int Y2 { get; set; }
}