如何为XDocument设置根节点

时间:2015-08-18 02:21:57

标签: c# xml linq

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

3 个答案:

答案 0 :(得分:0)

由于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 :(得分:0)

您可以通过多种方式解决问题。

选项1

使用XPathSelectElementsXpath表达式来缓解您的情况。

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

对您的代码进行少许修改,以便在这种情况下不包含根元素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();

希望这有帮助!

答案 2 :(得分:0)

我想要一本字典使用这个

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

        }
    }
}
​