简体   繁体   中英

How can I find and replace the value of an xml node?

Firstly, I am new to XML and C# . I need some pointers with finding and replacing an xml node value.

I have an XML file which has a set of input parameters. I need to find a specific input parameter ( <parameter id="PRP_AsOfDate"> ) in the file and then update an associated value ( <UCDateEndGetString>27/06/2011</UCDateEndGetString> ).

This will need to done daily so I need to replace the <UCDateEndGetString> with a tag and replace that.

I have tried the following code but get an illegal character error.

Can anyone help please?

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;

I am using .Net 4.0 and C# .

Here is some of my xml file:

<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>

I would give this article a quick read. Its very easy to follow and I believe it covers exactly what you are trying to accomplish.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM