2009-12-23 109 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");