如何更改 XML 文档中属性的值? [英] How to change the value of an attribute in an XML document?

查看:27
本文介绍了如何更改 XML 文档中属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个 XML 文档,在这个标签旁边有一个名为 的标签,它作为一个名为 FormId="d617a5e8-b49b-4640 的属性-9734-bc7a2bf05691"

I have an XML document below and there is a tag called <FormData> in side this tag it as an attribute called FormId="d617a5e8-b49b-4640-9734-bc7a2bf05691"

我想在 C# 代码中更改该值?

I would like to change that value in C# code?

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(MapPath(tempFolderPathAlt + "dvforms" + "\\XmlDataTemplate.xml"));
    //Change value of FormID
    xmlDoc.Save(tempFolderPath + "data.xml");

Be 是我的 XML 文档:

Be is my XML document:

<?xml version="1.0"?>
<FormData Platform="Android" PlatformVersion="100" Version="966" DataVersion="1" Description="Investec - Res" FormId="d617a5e8-b49b-4640-9734-bc7a2bf05691" FileId="e6202ba2-3658-4d8e-836a-2eb4902d441d" EncryptionVerification="" CreatedBy="Bob" EditedBy="Bob">
<FieldData>
<request_details_export_template Mod="20010101010101" IncludeInPDFExport="Yes"></request_details_export_template>
<request_details_reason_for_valuatio Mod="20010101010101" IncludeInPDFExport="Yes"></request_details_reason_for_valuatio>
</FieldData>
<Photos Mod="20010101010101"/>
<VoiceNotes/>
<Drawings Mod="20010101010101"/>
<FieldNotes/>
</FormData>

推荐答案

有几种方法可以做到这一点,包括:

There are several ways of doing this, including:

XmlAttribute formId = (XmlAttribute)xmlDoc.SelectSingleNode("//FormData/@FormId");
if (formId != null)
{
    formId.Value = "newValue"; // Set to new value.
}

或者这个:

XmlElement formData = (XmlElement)xmlDoc.SelectSingleNode("//FormData");
if (formData != null)
{
    formData.SetAttribute("FormId", "newValue"); // Set to new value.
}

SelectSingleNode 方法使用 XPath 来查找节点;这里有一个关于 XPath 的很好的教程.使用 SetAttribute 意味着 FormId 属性在它不存在时将被创建,如果它已经存在则更新.

The SelectSingleNode method uses XPath to find the node; there is a good tutorial about XPath here. Using SetAttribute means the FormId attribute will be created if it does not already exist, or updated if it does already exist.

在这种情况下,FormData 恰好是文档的根元素,因此您也可以这样做:

In this case, FormData happens to be the document's root element, so you can also do this:

xmlDoc.DocumentElement.SetAttribute("FormId", "newValue"); // Set to new value.

最后一个示例仅适用于您正在更改的节点恰好是文档中的根元素.

This last example will only work where the node you are changing happens to be the root element in the document.

要匹配特定的 FormId guid(不清楚这是否是您想要的):

To match a specific FormId guid (it is not clear if this is what you wanted):

XmlElement formData = (XmlElement)xmlDoc.SelectSingleNode("//FormData[@FormId='d617a5e8-b49b-4640-9734-bc7a2bf05691']");
if (formData != null)
{
    formData.SetAttribute("FormId", "newValue"); // Set to new value.
}

请注意,最后一个示例中的选择返回 FormData 元素而不是 FormId 属性;[] 括号中的表达式使我们能够搜索具有特定匹配属性的节点.

Note that the select in this last example returns the FormData element and not the FormId attribute; the expression in [] brackets enables us to search for a node with a particular matching attribute.

这篇关于如何更改 XML 文档中属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆