2009-12-23 111 views
2

我有一个包含2,000多个文件标签的WIX XML文档。我正在尝试使用LINQ to XML来创建程序,该程序可以更新每个文件标签的属性。我的代码如下,用于将当前属性加载到字典中。LINQ to XML和WIX问题

XElement root = XElement.Load(filePath); 
XNamespace wix = @"http://schemas.microsoft.com/wix/2006/wi"; 

IEnumerable<string> fileId = 
     from seg in root.Descendants(wix + "File") 
     select (string)seg.Attribute(wix + "Id"); 

IEnumerable<string> path = 
     from seg in root.Descendants(wix + "File") 
     select (string)seg.Attribute(wix + "Source"); 

string[] Position1 = fileId.ToArray(); 
string[] Position2 = path.ToArray(); 

for (int i = 0; i < Position1.Length; i++) 
{ 
     xmlDataRaw.Add(Position1[i], Position2[i]); 
} 

现在的问题是,我的计划说了IEnumerable FILEID和路径都含有唯一的“空”,但我知道该文件的标签是否存在,他们中的每一个都有一个ID和源属性。思考?

+0

检查您的命名空间是正确的。 – SLaks 2009-12-23 13:17:52

回答

3

属性不需要命名空间,尝试:

IEnumerable<string> fileId = 
     from seg in root.Descendants(wix + "File") 
     select (string)seg.Attribute("Id"); 

IEnumerable<string> path = 
     from seg in root.Descendants(wix + "File") 
     select (string)seg.Attribute("Source"); 
+0

你是现在最酷的人!那就是诀窍。谢谢! – Adkins 2009-12-23 13:23:04

+1

被之前咬过:/ – 2009-12-23 13:24:26

1

试图访问该属性时,不要使用命名空间。我注意到当你使用Descendant和Element等方法时,你只需要命名空间。

所以,你的代码应该是(例如)

IEnumerable<string> fileId = 
     from seg in root.Descendants(wix + "File") 
     select (string)seg.Attribute("Id");