LINQ to XML 和 WIX 问题

发布于 2024-08-16 02:47:35 字数 787 浏览 4 评论 0原文

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

 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 和路径都只包含“null”,但我知道文件标记存在,并且每个标记都有一个 ID 和 Source 属性。想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-08-23 02:47:35

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

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");

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");
匿名的好友 2024-08-23 02:47:35

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

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

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文