从 XML 个文件中查找并替换值
Find and substitute values from XML files
我正在尝试自动将翻译从一个 XML 文件替换为另一个文件。原翻译者将翻译设置在错误的文件中,我正在尝试通过自动化流程将其恢复。原始文件是这样的:
<version major="3" minor="6" revision="3" build="1" />
<region id="TranslatedStringKeys">
<node id="root">
<children>
<node id="TranslatedStringKey">
<attribute id="Content" type="28" handle="ls::TranslatedStringRepository::s_HandleUnknown" value="Spanish 1" />
<attribute id="ExtraData" type="23" value="" />
<attribute id="Speaker" type="22" value="" />
<attribute id="Stub" type="19" value="True" />
<attribute id="UUID" type="22" value="AAA" />
</node>
<node id="TranslatedStringKey">
<attribute id="Content" type="28" handle="ls::TranslatedStringRepository::s_HandleUnknown" value="Spanish 2" />
<attribute id="ExtraData" type="23" value="" />
<attribute id="Speaker" type="22" value="" />
<attribute id="Stub" type="19" value="True" />
<attribute id="UUID" type="22" value="BBB" />
</node>
</children>
</node>
</region>
<content contentuid="h5f6c914fg7db0g4763g9731g58a5eb60c6ab" Source="1.lsb" Key="AAA">English1</content>
<content contentuid="h95735cfdgc22cg4d38g9679ge071f18d77aa" Source="1.lsb" Key="BBB">English2</content>
我的目标是将属性which id="UUID"中的属性'value'的值与每个'content'节点的Key进行比较,如果相同则替换每个 'content' 节点的值与属性 which id="Content" 中的属性 'value' 的值,因此它的结尾如下:
<content contentuid="h5f6c914fg7db0g4763g9731g58a5eb60c6ab" Source="1.lsb" Key="AAA">Spanish 1</content>
<content contentuid="h95735cfdgc22cg4d38g9679ge071f18d77aa" Source="1.lsb" Key="BBB">Spanish 2</content>
我曾尝试使用 C# 和 Xml.Linq 进行操作,但由于我的经验非常有限,因此我在构建代码时遇到了很多错误。
感谢您的帮助和时间
如果有人遇到同样的问题,我找到了解决方法:
XmlDocument docMain = new XmlDocument();
XmlDocument docCommon = new XmlDocument();
docMain.Load("spanish.xml");
docCommon.Load("COMMON.xml");
foreach (XmlElement elem in docMain.FirstChild)
{
if (elem.GetAttribute("Key") != "")
{
foreach (XmlElement att in docCommon.SelectNodes("descendant::save/region/node/children/node"))
{
XmlNodeList AttList = (XmlNodeList)att.ChildNodes;
XmlElement trans = (XmlElement) AttList.Item(0);
XmlElement UUID = (XmlElement)AttList.Item(4);
if (elem.GetAttribute("Key") == UUID.GetAttribute("value"))
{
elem.InnerText= trans.GetAttribute("value");
}
else { }
}
}
else { }
}
docMain.Save("modified.xml");
如果您愿意,请随时添加建议以优化代码。
我正在尝试自动将翻译从一个 XML 文件替换为另一个文件。原翻译者将翻译设置在错误的文件中,我正在尝试通过自动化流程将其恢复。原始文件是这样的:
<version major="3" minor="6" revision="3" build="1" />
<region id="TranslatedStringKeys">
<node id="root">
<children>
<node id="TranslatedStringKey">
<attribute id="Content" type="28" handle="ls::TranslatedStringRepository::s_HandleUnknown" value="Spanish 1" />
<attribute id="ExtraData" type="23" value="" />
<attribute id="Speaker" type="22" value="" />
<attribute id="Stub" type="19" value="True" />
<attribute id="UUID" type="22" value="AAA" />
</node>
<node id="TranslatedStringKey">
<attribute id="Content" type="28" handle="ls::TranslatedStringRepository::s_HandleUnknown" value="Spanish 2" />
<attribute id="ExtraData" type="23" value="" />
<attribute id="Speaker" type="22" value="" />
<attribute id="Stub" type="19" value="True" />
<attribute id="UUID" type="22" value="BBB" />
</node>
</children>
</node>
</region>
<content contentuid="h5f6c914fg7db0g4763g9731g58a5eb60c6ab" Source="1.lsb" Key="AAA">English1</content>
<content contentuid="h95735cfdgc22cg4d38g9679ge071f18d77aa" Source="1.lsb" Key="BBB">English2</content>
我的目标是将属性which id="UUID"中的属性'value'的值与每个'content'节点的Key进行比较,如果相同则替换每个 'content' 节点的值与属性 which id="Content" 中的属性 'value' 的值,因此它的结尾如下:
<content contentuid="h5f6c914fg7db0g4763g9731g58a5eb60c6ab" Source="1.lsb" Key="AAA">Spanish 1</content>
<content contentuid="h95735cfdgc22cg4d38g9679ge071f18d77aa" Source="1.lsb" Key="BBB">Spanish 2</content>
我曾尝试使用 C# 和 Xml.Linq 进行操作,但由于我的经验非常有限,因此我在构建代码时遇到了很多错误。
感谢您的帮助和时间
如果有人遇到同样的问题,我找到了解决方法:
XmlDocument docMain = new XmlDocument();
XmlDocument docCommon = new XmlDocument();
docMain.Load("spanish.xml");
docCommon.Load("COMMON.xml");
foreach (XmlElement elem in docMain.FirstChild)
{
if (elem.GetAttribute("Key") != "")
{
foreach (XmlElement att in docCommon.SelectNodes("descendant::save/region/node/children/node"))
{
XmlNodeList AttList = (XmlNodeList)att.ChildNodes;
XmlElement trans = (XmlElement) AttList.Item(0);
XmlElement UUID = (XmlElement)AttList.Item(4);
if (elem.GetAttribute("Key") == UUID.GetAttribute("value"))
{
elem.InnerText= trans.GetAttribute("value");
}
else { }
}
}
else { }
}
docMain.Save("modified.xml");
如果您愿意,请随时添加建议以优化代码。