簡體   English   中英

如何設置XDocument的根節點

[英]How to set the Root Node for XDocument

我是LINQ to XML的新手。 我需要從XML Web配置獲取項目列表。 作為,我試圖獲取項目列表,但無法找到根節點。 下面是我的xml。

XML

<Vib>
   <SystemReports>
  <ReportUrl path="http://lenovo-pc/Report" ReportFolder="/AllSSRSReports" ExportFilePath="E:\Vib\" />
  <ImageFolder path="D:\Images" />

  <MailingLabels AvailableBarcode="Code39,Code93,CodeQR">
    <MailingLabel Type="L7654" Width="45.7" Height="25.4" HorizontalGapWidth="2.6" VerticalGapHeight="0" PageMarginTop="21.4" PageMarginBottom="21.4" PageMarginLeft="9.7" PageMarginRight="9.7" PageSize="A4" LabelsPerRow="4" LabelRowsPerPage="10" />
    <MailingLabel Type="L7656" Width="46" Height="11.1" HorizontalGapWidth="4.7" VerticalGapHeight="1.6" PageMarginTop="15.9" PageMarginBottom="15.9" PageMarginLeft="6" PageMarginRight="6" PageSize="A4" LabelsPerRow="4" LabelRowsPerPage="21" />
    <MailingLabel Type="L7160" Width="63.5" Height="38.1" HorizontalGapWidth="2.5" VerticalGapHeight="0" PageMarginTop="15.1" PageMarginBottom="15.1" PageMarginLeft="7.2" PageMarginRight="7.2" PageSize="A4" LabelsPerRow="3" LabelRowsPerPage="7" />
  </MailingLabels>

</SystemReports>
</Vib>

var dictionary = (from t in xdo.Root.Element("Vib").Element("SystemReports").Element("MailingLabels").Elements("MailingLabel")
                          select new
                          {
                              Type = (string)t.Attribute("Type"),
                              Width = (string)t.Attribute("Width"),
                              HorizontalGapWidth = (string)t.Attribute("HorizontalGapWidth"),
                              VerticalGapHeight = (string)t.Attribute("VerticalGapHeight"),
                              PageMarginTop = (string)t.Attribute("PageMarginTop"),
                              PageMarginBottom = (string)t.Attribute("PageMarginBottom"),
                              PageMarginLeft = (string)t.Attribute("PageMarginLeft"),
                              PageMarginRight = (string)t.Attribute("PageMarginRight"),
                              PageSize = (string)t.Attribute("PageSize"),
                              LabelsPerRow = (string)t.Attribute("LabelsPerRow"),
                              LabelRowsPerPage = (string)t.Attribute("LabelRowsPerPage")
                          }).ToList();

由於xml中的根是Vib因此您需要刪除Element("Vib") 只需訪問第一個孩子,即SystemReports

var dictionary = (from t in xdo.Root.Element("SystemReports").Element("MailingLabels").Elements("MailingLabel")
                              select new
                              {
                                  Type = (string)t.Attribute("Type"),
                                  Width = (string)t.Attribute("Width"),
                                  HorizontalGapWidth = (string)t.Attribute("HorizontalGapWidth"),
                                  VerticalGapHeight = (string)t.Attribute("VerticalGapHeight"),
                                  PageMarginTop = (string)t.Attribute("PageMarginTop"),
                                  PageMarginBottom = (string)t.Attribute("PageMarginBottom"),
                                  PageMarginLeft = (string)t.Attribute("PageMarginLeft"),
                                  PageMarginRight = (string)t.Attribute("PageMarginRight"),
                                  PageSize = (string)t.Attribute("PageSize"),
                                  LabelsPerRow = (string)t.Attribute("LabelsPerRow"),
                                  LabelRowsPerPage = (string)t.Attribute("LabelRowsPerPage")
                              }).ToList();

在這里嘗試: https : //dotnetfiddle.net/tl03de

您有多種選擇來解決您的問題。

選項1

使用XPathSelectElements ,這是一個Xpath表達式來減輕您的XPathSelectElements

var dictionary = (from t in xdo.XPathSelectElements("/Vib/SystemReports/MailingLabels/MailingLabel")
select new 
{
    Type = (string) t.Attribute("Type"),
    Width = (string) t.Attribute("Width"),
    HorizontalGapWidth = (string) t.Attribute("HorizontalGapWidth"),
    VerticalGapHeight = (string) t.Attribute("VerticalGapHeight"),
    PageMarginTop = (string) t.Attribute("PageMarginTop"),
    PageMarginBottom = (string) t.Attribute("PageMarginBottom"),
    PageMarginLeft = (string) t.Attribute("PageMarginLeft"),
    PageMarginRight = (string) t.Attribute("PageMarginRight"),
    PageSize = (string) t.Attribute("PageSize"),
    LabelsPerRow = (string) t.Attribute("LabelsPerRow"),
    LabelRowsPerPage = (string) t.Attribute("LabelRowsPerPage")
}).ToList();

選項2

對您的代碼進行很少的修改即可在這種情況下不包含Root元素Vib

var dictionary = (from t in xdo.Root.Element("SystemReports").Element("MailingLabels").Elements("MailingLabel")
select new 
{
    Type = (string) t.Attribute("Type"),
    Width = (string) t.Attribute("Width"),
    HorizontalGapWidth = (string) t.Attribute("HorizontalGapWidth"),
    VerticalGapHeight = (string) t.Attribute("VerticalGapHeight"),
    PageMarginTop = (string) t.Attribute("PageMarginTop"),
    PageMarginBottom = (string) t.Attribute("PageMarginBottom"),
    PageMarginLeft = (string) t.Attribute("PageMarginLeft"),
    PageMarginRight = (string) t.Attribute("PageMarginRight"),
    PageSize = (string) t.Attribute("PageSize"),
    LabelsPerRow = (string) t.Attribute("LabelsPerRow"),
    LabelRowsPerPage = (string) t.Attribute("LabelRowsPerPage")
}).ToList();

希望這可以幫助 !

我要一本字典用這個

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Vib>" +
                   "<SystemReports>" +
                  "<ReportUrl path=\"http://lenovo-pc/Report\" ReportFolder=\"/AllSSRSReports\" ExportFilePath=\"E:\\Vib\\\" />" +
                  "<ImageFolder path=\"D:\\Images\" />" +
                  "<MailingLabels AvailableBarcode=\"Code39,Code93,CodeQR\">" +
                    "<MailingLabel Type=\"L7654\" Width=\"45.7\" Height=\"25.4\" HorizontalGapWidth=\"2.6\" VerticalGapHeight=\"0\" PageMarginTop=\"21.4\" PageMarginBottom=\"21.4\" PageMarginLeft=\"9.7\" PageMarginRight=\"9.7\" PageSize=\"A4\" LabelsPerRow=\"4\" LabelRowsPerPage=\"10\" />" +
                    "<MailingLabel Type=\"L7656\" Width=\"46\" Height=\"11.1\" HorizontalGapWidth=\"4.7\" VerticalGapHeight=\"1.6\" PageMarginTop=\"15.9\" PageMarginBottom=\"15.9\" PageMarginLeft=\"6\" PageMarginRight=\"6\" PageSize=\"A4\" LabelsPerRow=\"4\" LabelRowsPerPage=\"21\" />" +
                    "<MailingLabel Type=\"L7160\" Width=\"63.5\" Height=\"38.1\" HorizontalGapWidth=\"2.5\" VerticalGapHeight=\"0\" PageMarginTop=\"15.1\" PageMarginBottom=\"15.1\" PageMarginLeft=\"7.2\" PageMarginRight=\"7.2\" PageSize=\"A4\" LabelsPerRow=\"3\" LabelRowsPerPage=\"7\" />" +
                  "</MailingLabels>" +
                "</SystemReports>" +
                "</Vib>";

            XDocument xdo = XDocument.Parse(input);
            var dictionary = (from t in xdo.Descendants("MailingLabel")
                              select new
                              {
                                  Type = (string)t.Attribute("Type"),
                                  Width = (string)t.Attribute("Width"),
                                  HorizontalGapWidth = (string)t.Attribute("HorizontalGapWidth"),
                                  VerticalGapHeight = (string)t.Attribute("VerticalGapHeight"),
                                  PageMarginTop = (string)t.Attribute("PageMarginTop"),
                                  PageMarginBottom = (string)t.Attribute("PageMarginBottom"),
                                  PageMarginLeft = (string)t.Attribute("PageMarginLeft"),
                                  PageMarginRight = (string)t.Attribute("PageMarginRight"),
                                  PageSize = (string)t.Attribute("PageSize"),
                                  LabelsPerRow = (string)t.Attribute("LabelsPerRow"),
                                  LabelRowsPerPage = (string)t.Attribute("LabelRowsPerPage")
                              }).GroupBy(x => x.Type, y => y)
                              .ToDictionary(x => x.Key, y => y.ToList());

        }
    }
}
​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM