Linq to xml 获取列表列表
Linq to xml get list of lists
我有一个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>
之前我没有 <Contour>
元素。
我的代码是
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);
}
我不明白如何获取每个照片列表的行列表。
我是 linq 的新手,有人能告诉我正确的做法吗?
我宁愿使用 Xml 反序列化,但使用 Linq2XML 它看起来像这样(对 show/prove 功能使用单元测试):
[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; }
}
我有一个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>
之前我没有 <Contour>
元素。
我的代码是
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);
}
我不明白如何获取每个照片列表的行列表。 我是 linq 的新手,有人能告诉我正确的做法吗?
我宁愿使用 Xml 反序列化,但使用 Linq2XML 它看起来像这样(对 show/prove 功能使用单元测试):
[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; }
}