簡體   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