【问题标题】:How can I find and replace the value of an xml node?如何查找和替换 xml 节点的值?
【发布时间】:2011-07-05 11:47:04
【问题描述】:

首先,我是XMLC# 的新手。我需要一些指针来查找和替换 xml 节点值。

我有一个XML 文件,其中包含一组输入参数。我需要在文件中找到一个特定的输入参数(<parameter id="PRP_AsOfDate">),然后更新一个关联的值(<UCDateEndGetString>27/06/2011</UCDateEndGetString>)。

这需要每天完成,所以我需要用标签替换 <UCDateEndGetString> 并替换它。

我尝试了以下代码,但出现非法字符错误。

有人可以帮忙吗?

public string TestRequestDef()
    {
        string reqtype = "Position";
        GetRequestDef getxml = new GetRequestDef();
        string xmlcall = getxml.GetXMLRequest(reqtype);

        XmlDocument doc = new XmlDocument();
        doc.Load(xmlcall);

        string newValue = "XXXX";
        XmlNode paraNode = doc.SelectSingleNode("parameters/parameter/UCValue/UCPitDateGetString");
        paraNode.Attributes[0].Value = newValue;
        doc.Save(xmlcall);

        return xmlcall;

我正在使用.Net 4.0C#

这是我的一些 xml 文件:

<parameters>
        <parameter id="PRP_AsOfDate">
            <deoId/>
            <groupBy>false</groupBy>
            <dateControlVisible>true</dateControlVisible>
            <pitControlVisible>true</pitControlVisible>
            <excludeFromOutput>false</excludeFromOutput>
            <multiCurrencyRequired>false</multiCurrencyRequired>
            <label>As Of Date</label>
            <style type="Date">
                <maxSelect>0</maxSelect>
                <minSelect>0</minSelect>
                <level/>
                <content/>
                <fieldTypes/>
                <dataObjectType>Date</dataObjectType>
                <filter/>
                <value/>
                <formatString/>
            </style>
            <formatString/>
            <validations/>
            <UCValue>
                <UCComposites/>
                <UCIdGetString/>
                <UCNameGetString/>
                <UCDateDays>0</UCDateDays>
                    <UCDateStartGetString>01/01/1900</UCDateStartGetString>
                <UCDateEndGetString>27/06/2011</UCDateEndGetString>
                <UCPitDateGetString>26/06/2011 00:00:00</UCPitDateGetString>
                <UCPitDatePreviousGetString>26/06/2011 00:00:00</UCPitDatePreviousGetString>
                <UCPitChecked>True</UCPitChecked>
                <UCDateOptionGetString>SPECIFIC</UCDateOptionGetString>
                <UCDefaultValueGetString>True|SPECIFIC|27/06/2011|</UCDefaultValueGetString>
                <UCValueGetString>True|SPECIFIC|27/06/2011|</UCValueGetString>
            </UCValue>
        </parameter>

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    我会快速阅读这篇文章。它很容易理解,我相信它完全涵盖了您想要完成的任务。

    http://www.codeproject.com/KB/cpp/myXPath.aspx

    【讨论】:

    • 谢谢丹尼尔,这正是我想要的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 2018-03-05
    • 2013-07-11
    • 2016-10-31
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多