繁体   English   中英

从XML C#获取子查询列表

[英]Get subquery list from XML c#

我正在编写一个从XML读取并将代码放在列表中的代码。 信息:

  1. 员工姓名
  2. 他已经完成的几个月
  3. 他需要继续工作的几个月。

它们都存在于XML文件中。

我写了这段代码:

List<MonthlyInformation> result =
                doc.Root.Elements().Descendants(nsXml + "MitarbeiterGroup")
                .Select(x => new MonthlyInformation
                {
                    Firstname = (string)x.Attribute("Group9"),
                    FinishedMonths = x.Descendants("Monat3").Select(s => new FinishedMonth {MonthName = (string)s.Attribute("Monat3"), Money = (string)s.Element("Cell").Attribute("Textbox142") }).ToList(),
                    ForecastMonths = x.Descendants("Monat9").Select(s => new ForecastMonth { MonthName = (string)s.Attribute("Monat9"), Money = (string)s.Element("Cell").Attribute("Textbox143") }).ToList()
                }).ToList();

该代码可以正常工作,但FinishedMonths和ForecastMonths数据成员始终为空。

这是XML的一部分

<MitarbeiterGroup Group9="Name....">
            <Textbox111>
              <UmsatzInternGroup_Collection>
                <UmsatzInternGroup>
                  <Monat3 Monat3="Jan.">
                    <Cell Textbox142="11325" />
                  </Monat3>
                </UmsatzInternGroup>
                <UmsatzInternGroup>
                  <Monat3 Monat3="Feb.">
                    <Cell Textbox142="12345" />
                  </Monat3>
                </UmsatzInternGroup>
              </UmsatzInternGroup_Collection>
               <ForecastExternGroup_Collection>
                <ForecastExternGroup>
                  <Monat9 Monat9="Sep.">
                    <Cell Textbox143="17130" />
                  </Monat9>
                </ForecastExternGroup>
                <ForecastExternGroup>
                  <Monat9 Monat9="Okt.">
                    <Cell Textbox143="18000" />
                  </Monat9>
                </ForecastExternGroup>
              </ForecastExternGroup_Collection>
            </Textbox111>
      </MitarbeiterGroup>

因此,我需要在“ Monat3”中为每个员工获取所有月份,在“ Monat9”中获取所有预测月份。

请您尽快提供帮助

我不确定您的nsXml变量是什么样,但是将这一行doc.Root.Elements().Descendants(nsXml + "MitarbeiterGroup")更改为doc.Root.Elements()

为我工作。 在此处输入图片说明

也许您的ns不正确?

编辑:

这是我使用的XML

<MitarbeiterGroup Group9="Name....">
    <Textbox111>
        <UmsatzInternGroup_Collection>
            <UmsatzInternGroup>
                <Monat3 Monat3="Jan.">
                    <Cell Textbox142="11325" />
                </Monat3>
            </UmsatzInternGroup>
            <UmsatzInternGroup>
                <Monat3 Monat3="Feb.">
                    <Cell Textbox142="12345" />
                </Monat3>
                <ForecastExternGroup_Collection>
                    <ForecastExternGroup>
                        <Monat9 Monat9="Sep.">
                            <Cell Textbox143="17130" />
                        </Monat9>
                    </ForecastExternGroup>
                    <ForecastExternGroup>
                        <Monat9 Monat9="Okt.">
                            <Cell Textbox143="18000" />
                        </Monat9>
                    </ForecastExternGroup>
                </ForecastExternGroup_Collection>
            </UmsatzInternGroup>
        </UmsatzInternGroup_Collection>
    </Textbox111>
</MitarbeiterGroup>

编辑:

使用doc.Descendants("MitarbeiterGroup")代替doc.Root.Elements().Descendants()

我相信这与Elements()的工作方式有关。 如果您比较以下两个:

var descendants = doc.Descendants().ToList();
var elements = doc.Elements().ToList();

您可以看到Descendants()是所有子级的平面列表,其中Elements()是像树一样的层次结构,即使您正在调用Descendants() ,也已经调用了Elements()

编辑:

在lambda内部,您再次调用x.Descendants() ,而不是像x.Descendants("Monat3")x.Descendants(XName.Get("Monat3"))它,它需要完全限定(?确定在术语上) ,它应该看起来像x.Descendants(XName.Get("Monat3", ns))

string testURL = "XML.xml";
XDocument doc = XDocument.Load(testURL);
string ns = doc.Root.GetDefaultNamespace().ToString();
List<MonthlyInformation> result =
doc.Descendants(XName.Get("MitarbeiterGroup", ns))
.Select(x =>
new MonthlyInformation
{
    Name = (string)x.Attribute("Group9"),
    FinishedMonths = x.Descendants(XName.Get("Monat3", ns)).Select(s => new FinishedMonth
    {
        MonthName = (string)s.Attribute("Monat3"),
        Money = "money" //(string)s.Element("Cell").Attribute("Textbox142") 
    }).ToList(),
    ForecastMonths = x.Descendants(XName.Get("Monat9", ns)).Select(s => new ForecastMonth
    {
        MonthName = (string)s.Attribute("Monat9"),
        Money = "money" //(string)s.Element("Cell").Attribute("Textbox143")
    }).ToList()
}).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM