如何加入xml

发布于 2024-11-26 03:16:41 字数 4688 浏览 2 评论 0原文

我有 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,

                        };

此代码加入 CalendarFairExecutive 现在如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2024-12-03 03:16:41

在我看来,你只需要:

 join d in doc3.Descendants("FairCenter")
 on b.Element("IdFair").Value equals d.Element("Id").Value

是什么给你带来了问题?请注意,您实际上并不需要所有这些 let 语句来进行连接。我认为这样会更清楚:

var test = from b in doc.Descendants("CalendarFair")
           where ...
           join c in doc1.Descendants("Executive")
             on b.Element("IdExecutive").Value equals c.Element("Id").Value
           join d in doc3.Descendants("FairCenter")
             on b.Element("IdFair").Value equals d.Element("Id").Value  

这使得您在每个阶段匹配哪些元素变得非常清楚。

我假设您也想在投影中使用 d ...

(您可能应该考虑将 DateStart 转换为 DateTime 而不是顺便说一句,而不是使用所有这些子字符串操作。)

It looks to me like you just need:

 join d in doc3.Descendants("FairCenter")
 on b.Element("IdFair").Value equals d.Element("Id").Value

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:

var test = from b in doc.Descendants("CalendarFair")
           where ...
           join c in doc1.Descendants("Executive")
             on b.Element("IdExecutive").Value equals c.Element("Id").Value
           join d in doc3.Descendants("FairCenter")
             on b.Element("IdFair").Value equals d.Element("Id").Value  

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 a DateTime rather than using all those substring operations, by the way.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文