简体   繁体   English

解析节点C#中的节点的Xml

[英]Parse Xml For Nodes Within Nodes C#

What am I doing wrong in my xml query below? 我在下面的xml查询中怎么了?

I am attempting to print all "AppUserModelID" values in this xml. 我正在尝试在此xml中打印所有“ AppUserModelID”值。 I am drilling down to Groups then attempting to loop through the Groups. 我正在深入研究组,然后尝试遍历组。 Is there a better way to do this than using a long drill down string to get to the Groups? 有比使用长的向下钻取字符串进入组更好的方法吗?

   <LayoutModificationTemplate Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
      <LayoutOptions StartTileGroupCellWidth="6" />
      <DefaultLayoutOverride>
        <StartLayoutCollection>
          <defaultlayout:StartLayout GroupCellWidth="6" xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout">
            <start:Group Name="Life at a glance" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout">
              <start:Tile Size="2x2" Column="0" Row="0" AppUserModelID="microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.calendar" />
              <start:Tile Size="4x2" Column="2" Row="0" AppUserModelID="microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.mail" />
              <start:Tile Size="2x2" Column="0" Row="2" AppUserModelID="Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" />
              <start:Tile Size="2x2" Column="2" Row="2" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
              <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="Microsoft.Windows.Cortana_cw5n1h2txyewy!CortanaUI" />
              <start:Tile Size="2x2" Column="0" Row="4" AppUserModelID="Microsoft.BingWeather_8wekyb3d8bbwe!App" />
              <start:Tile Size="2x2" Column="2" Row="4" AppUserModelID="Microsoft.WindowsPhone_8wekyb3d8bbwe!CompanionApp.App" />
              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim" />
            </start:Group>
            <start:Group Name="Play and explore" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout">
              <start:Tile Size="4x2" Column="2" Row="2" AppUserModelID="Microsoft.BingNews_8wekyb3d8bbwe!AppexNews" />
              <start:Tile Size="2x2" Column="0" Row="4" AppUserModelID="Microsoft.WindowsStore_8wekyb3d8bbwe!App" />
              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe!Microsoft.MicrosoftOfficeHub" />
            </start:Group>
            <start:Group Name="" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout">
              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="Microsoft.InternetExplorer.Default" />
              <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationID="VMware.Horizon.Client" />
              <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" DesktopApplicationID="Microsoft.Office.OUTLOOK.EXE.16" />
            </start:Group>
            <start:Group Name="" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout">
              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="C:\Users\jason\Documents" />
              <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" DesktopApplicationID="Chrome" />
            </start:Group>
          </defaultlayout:StartLayout>
        </StartLayoutCollection>
      </DefaultLayoutOverride>
    </LayoutModificationTemplate>

Here is my code: 这是我的代码:

    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\Users\Nick\Desktop\hi.xml");
    //XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);

    XmlNodeList groups = doc.DocumentElement.SelectNodes("/LayoutModificationTemplate/LayoutOptions/DefaultLayoutOverride/StartLayoutCollection/defaultlayout:StartLayout/start:Group"
                                                         //, manager
                                                         );
    List<String> pinnedProgram = new List<String>();

    foreach (XmlNode group in groups)
    {
        XmlNodeList titles = doc.DocumentElement.SelectNodes("/start:Tile");

        foreach (XmlNode title in titles)
        {
            pinnedProgram.Add(title.Attributes["AppUserModelID"].Value);
        }

    }
}

Use xml linq : 使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement startLayout = doc.Descendants().Where(x => x.Name.LocalName == "StartLayout").FirstOrDefault();

            var results = startLayout.Elements().Where(x => x.Name.LocalName == "Group").Select(x => new {
                name = (string)x.Attribute("Name"),
                tiles = x.Elements().Select(y => new {
                    size= (string)y.Attribute("Size"),
                    column = (int)y.Attribute("Column"),
                    row = (int)y.Attribute("Row"),
                    modelId = (string)y.Attribute("AppUserModelID"),
                    appId = (string)y.Attribute("DesktopApplicationID")
                }).ToList()
            }).ToList();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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