我在下面的xml查询中做错了什么?
我正在尝试在此xml中打印所有“AppUserModelID”值。 我正在深入到Group,然后尝试遍历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>
这是我的代码:
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);
}
}
}
答案 0 :(得分:2)
使用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();
}
}
}