LINQ To XML - 如何读取此 XML?

发布于 2024-10-23 16:31:04 字数 425 浏览 1 评论 0原文

我有一个如下所示的 XML 文件:

...
<body>

<unit id="1" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

<unit id="2" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

</body>

有人可以告诉我如何通过 C# 使用 LINQ to XML 来读取 sourceFile 节点的值,并更新 targetFile 的值,因为我不熟悉 LINQ to XML ?

谢谢。

I've got an XML file that looks like this:

...
<body>

<unit id="1" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

<unit id="2" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

</body>

Can someone show me how I would use LINQ to XML via C# to read the value of the sourceFile node, and update the value of targetFile as I'm not familiar with LINQ to XML?

Thanks.

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

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

发布评论

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

评论(2

别靠近我心 2024-10-30 16:31:04

像这样的东西:

XDocument doc = XDocument.Load("file.xml");
foreach (var sourceNode in doc.Descendants("sourceFile"))
{
    XElement targetNode = sourceNode.Parent.Element("targetFile");
    if (targetNode != null)
    {
        targetNode.Value = sourceNode.Value;
    }
}

或者:(当然

XDocument doc = XDocument.Load("file.xml");
foreach (var unitNode in doc.Descendants("unit"))
{
    XElement sourceNode = unitNode.Element("sourceFile");
    XElement targetNode = unitNode.Element("targetFile");
    if (sourceNode != null && targetNode != null)
    {
        targetNode.Value = sourceNode.Value;
    }
}

,如果您想保存回文件,请随后调用 doc.Save ,正如另一个答案中指出的那样。)

Something like this:

XDocument doc = XDocument.Load("file.xml");
foreach (var sourceNode in doc.Descendants("sourceFile"))
{
    XElement targetNode = sourceNode.Parent.Element("targetFile");
    if (targetNode != null)
    {
        targetNode.Value = sourceNode.Value;
    }
}

Alternatively:

XDocument doc = XDocument.Load("file.xml");
foreach (var unitNode in doc.Descendants("unit"))
{
    XElement sourceNode = unitNode.Element("sourceFile");
    XElement targetNode = unitNode.Element("targetFile");
    if (sourceNode != null && targetNode != null)
    {
        targetNode.Value = sourceNode.Value;
    }
}

(And call doc.Save afterwards if you want to save back to a file, of course, as pointed out in another answer.)

叶落知秋 2024-10-30 16:31:04

要更新实际文件本身,您只需对加载的文档调用 Save() 方法。

    string path = "yourfile.xml";
    XDocument doc = XDocument.Load(path);
    foreach (XElement unit in doc.Descendants("unit"))
    {
        XElement source = unit.Element("sourceFile");
        XElement target = unit.Element("targetFile");
        target.Value = source.Value;  // or whatever
    }
    doc.Save(path);

To update the actual file itself, you just need to call the Save() method on the loaded document.

    string path = "yourfile.xml";
    XDocument doc = XDocument.Load(path);
    foreach (XElement unit in doc.Descendants("unit"))
    {
        XElement source = unit.Element("sourceFile");
        XElement target = unit.Element("targetFile");
        target.Value = source.Value;  // or whatever
    }
    doc.Save(path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文