简体   繁体   English

如何查找和替换 xml 节点的值?

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

Firstly, I am new to XML and C# .首先,我是XMLC#的新手。 I need some pointers with finding and replacing an xml node value.我需要一些指针来查找和替换 xml 节点值。

I have an XML file which has a set of input parameters.我有一个XML文件,它有一组输入参数。 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> ).我需要在文件中找到一个特定的输入参数( <parameter id="PRP_AsOfDate"> ),然后更新一个关联的值( <UCDateEndGetString>27/06/2011</UCDateEndGetString> )。

This will need to done daily so I need to replace the <UCDateEndGetString> with a tag and replace that.这需要每天完成,所以我需要用标签替换<UCDateEndGetString>并替换它。

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# .我正在使用.Net 4.0C#

Here is some of my xml file:这是我的一些 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>

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 http://www.codeproject.com/KB/cpp/myXPath.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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