简体   繁体   中英

Get subquery list from XML c#

I am writing a code that reads from XML and put the information on the list. 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.

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.

here is a part of the 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".

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

worked for me. 在此处输入图片说明

Maybe you have the ns incorrect?

EDIT:

This is the XML I used

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

I beleive this has something to do with the way Elements() works. 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()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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