views:

56

answers:

2

I have a WIX XML document that contains 2,000+ file tags. I am trying to make a program using LINQ to XML that can update an attribute of each file tag. My code is as follows for loading the current attributes into a dictionary.

 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]);
 }

now the problem is that my program says that the IEnumerable fileID and path both contain only "null" but I know that the file tag exists and that every one of them has an ID and Source attribute. Thoughts?

+2  A: 

Attributes don't require namespaces, try:

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");
Rob Fonseca-Ensor
You are as of right now the coolest person alive! That did the trick. Thanks!
Adkins
Been bitten by that before :/
Rob Fonseca-Ensor
+1  A: 

Don't use the namespace when trying to access the Attribute. I've noticed that you only need the namespace when you're using methods like Descendant and Element.

So your code should be (for example)

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