简体   繁体   English

从XML C#获取子查询列表

[英]Get subquery list from XML c#

I am writing a code that reads from XML and put the information on the list. 我正在编写一个从XML读取并将代码放在列表中的代码。 the information: 信息:

  1. Employee Name 员工姓名
  2. the months he already finish 他已经完成的几个月
  3. the months he needs to work on. 他需要继续工作的几个月。

and they are all exist in XML File. 它们都存在于XML文件中。

I wrote this code : 我写了这段代码:

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();

the code works fine but both the FinishedMonths and ForecastMonths datamember are always empty. 该代码可以正常工作,但FinishedMonths和ForecastMonths数据成员始终为空。

here is a part of the XML 这是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>

so I need to get for every employee all the months in "Monat3" and all the forecast Months in "Monat9". 因此,我需要在“ Monat3”中为每个员工获取所有月份,在“ Monat9”中获取所有预测月份。

please if you can help as soon as possible 请您尽快提供帮助

I'm not sure what your nsXml variable looks like, however changing this line doc.Root.Elements().Descendants(nsXml + "MitarbeiterGroup") to doc.Root.Elements() 我不确定您的nsXml变量是什么样,但是将这一行doc.Root.Elements().Descendants(nsXml + "MitarbeiterGroup")更改为doc.Root.Elements()

worked for me. 为我工作。 在此处输入图片说明

Maybe you have the ns incorrect? 也许您的ns不正确?

EDIT: 编辑:

This is the XML I used 这是我使用的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>

EDIT: 编辑:

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

I beleive this has something to do with the way Elements() works. 我相信这与Elements()的工作方式有关。 If you compare the following two: 如果您比较以下两个:

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

You can see that Descendants() is a flat list of all the children where as Elements() is tree like hierarchy, and even though you are calling Descendants() you've already called Elements() 您可以看到Descendants()是所有子级的平面列表,其中Elements()是像树一样的层次结构,即使您正在调用Descendants() ,也已经调用了Elements()

EDIT: 编辑:

Inside the lambda where you again call x.Descendants() , instead of using calling it like x.Descendants("Monat3") or x.Descendants(XName.Get("Monat3")) it needs to be fully qualified (? not sure on the terminology) , it should look like x.Descendants(XName.Get("Monat3", ns)) 在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