【问题标题】:Find and substitute values from XML files从 XML 文件中查找和替换值
【发布时间】:2021-11-06 23:40:09
【问题描述】:

我正在尝试自动将翻译从一个 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>

我的目标是将 id="UUID" 属性中的属性 'value' 的值与每个 'content' 节点的 Key 进行比较,如果相同,则替换每个 'content' 的值' 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 进行操作,但由于我的代码构建经验非常有限,因此我在构建代码时遇到了很多错误。

感谢您的帮助和时间

【问题讨论】:

    标签: c# xml replace


    【解决方案1】:

    如果有人遇到同样的问题,我找到了解决方案:

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

    如果您愿意,请随时添加建议以优化代码。

    【讨论】:

    • 请将您的答案标记为答案,以便问题结束。
    猜你喜欢
    • 2017-08-07
    • 2014-08-17
    • 2016-10-31
    • 2019-10-16
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2010-12-20
    相关资源
    最近更新 更多