如何加入xml
我有 3 个 XML 文件,可以通过 id 加入。
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where
start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
};
此代码加入 CalendarFair
和 Executive
现在如何将 FairCenter
加入此代码
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on ........ //by id
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value
};
FairCenter.xml
<FairCenters>
<FairCenter>
<Name>c1</Name>
<Id>1</Id>
</FairCenter>
<FairCenter>
<Name>c2</Name>
<Id>1</Id>
</FairCenter>
Executives.xml
<Executives>
<Executive>
<NameExecutive>e1</NameExecutive>
<Id>1</Id>
</Executive>
<Executive>
<NameExecutive>e2</NameExecutive>
<Id>2</Id>
</Executive>
</Executives>
CalendarFair.xml
<CalendarFairs>
<CalendarFair>
<DateStart>09/09/2011</DateStart>
<DateEnd>07/15/2011</DateEnd>
<Title>f1</Title>
<IdExecutive>1</IdExecutive>
<IdCenter>1</IdCenter>
</CalendarFair>
<CalendarFair>
<DateStart>07/14/2011</DateStart>
<DateEnd>07/20/2011</DateEnd>
<Title>f2</Title>
<IdExecutive>5</IdExecutive>
<IdCenter>2</IdCenter>
</CalendarFair>
</CalendarFairs>
Answer
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) && start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
let id2 = b.Element("IdCenter").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on id2 equals d.Element("Id").Value
select new
{
Title = b.Element("Title").Value ,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value,
NameCenter = d.Element("Name").Value
};
I have 3 XML files I can join by id.
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where
start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
};
this code join CalendarFair
and Executive
now how can join FairCenter
to this code
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) &&
start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on ........ //by id
select new
{
Title = b.Element("Title").Value,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value
};
FairCenter.xml
<FairCenters>
<FairCenter>
<Name>c1</Name>
<Id>1</Id>
</FairCenter>
<FairCenter>
<Name>c2</Name>
<Id>1</Id>
</FairCenter>
Executives.xml
<Executives>
<Executive>
<NameExecutive>e1</NameExecutive>
<Id>1</Id>
</Executive>
<Executive>
<NameExecutive>e2</NameExecutive>
<Id>2</Id>
</Executive>
</Executives>
CalendarFair.xml
<CalendarFairs>
<CalendarFair>
<DateStart>09/09/2011</DateStart>
<DateEnd>07/15/2011</DateEnd>
<Title>f1</Title>
<IdExecutive>1</IdExecutive>
<IdCenter>1</IdCenter>
</CalendarFair>
<CalendarFair>
<DateStart>07/14/2011</DateStart>
<DateEnd>07/20/2011</DateEnd>
<Title>f2</Title>
<IdExecutive>5</IdExecutive>
<IdCenter>2</IdCenter>
</CalendarFair>
</CalendarFairs>
Answer
var test = from b in doc.Descendants("CalendarFair")
let start = b.Element("DateStart").Value
where start.Substring(0, 2) == dateNow.Substring(0, 2) && start.Substring(6, 4) == dateNow.Substring(6, 4)
let id1 = b.Element("IdExecutive").Value
let id2 = b.Element("IdCenter").Value
join c in doc1.Descendants("Executive")
on id1 equals c.Element("Id").Value
join d in doc3.Descendants("FairCenter")
on id2 equals d.Element("Id").Value
select new
{
Title = b.Element("Title").Value ,
DateStart=b.Element("DateStart").Value,
DateEnd=b.Element("DateEnd").Value,
NameExecutive = c.Element("NameExecutive").Value,
telExecutive = c.Element("Tel").Value,
websiteExecutive = c.Element("Website").Value,
NameCenter = d.Element("Name").Value
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你只需要:
是什么给你带来了问题?请注意,您实际上并不需要所有这些
let
语句来进行连接。我认为这样会更清楚:这使得您在每个阶段匹配哪些元素变得非常清楚。
我假设您也想在投影中使用
d
...(您可能应该考虑将
DateStart
转换为DateTime
而不是顺便说一句,而不是使用所有这些子字符串操作。)It looks to me like you just need:
What was giving you problems with that? Note that you don't really need all those
let
statements for the joins. I think it would be clearer as:That makes it really clear which elements you're matching at each stage.
I assume you'll want to use
d
in your projection, too...(You should probably look at casting
DateStart
to aDateTime
rather than using all those substring operations, by the way.)