0

I would like to edit xml data for one element with in SOAP request in order to send unique SOAP requests.

Following is the example request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:web="http://webservice/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:ca>
         <type1>
            <ad>2013-07-19</ad>
            <name>abcd 13071502</name>
            <taker>
               <taker>TEST</taker>
               <emailAddress>[email protected]</emailAddress>
               <name>nameTest</name>
               <phoneNo>007007007</phoneNo>
               <takerUid>1234</takerUid>
            </taker>
         </type1>
         <type2>4</type2>
         <type3>peace</type3>
         <type4>test</type4>
      </web:ca>
   </soapenv:Body>
</soapenv:Envelope>

I would like to change "name" element value from "abcd 13071502" to "abcd ". I was able to extract data from "name" element and edit the value by using following code in C#

System.Xml.XmlTextReader xr = new XmlTextReader(@filePath);
while (xr.Read())
{
if (xr.LocalName == "name")
    {
        xr.Read();
        currentNameValue = xr.Value;
        int cnvLen = currentNameValue.Length;
        string cnvWOdate = currentNameValue.Substring(0, cnvLen-8);
        string newNameValue = cnvWOdate+currTimeDate;
        break;
    }
}

However, I couldn't figure out how to edit the value and save the file. Any help would be appreciated. Thank you.

1
  • FYI, new XmlTextReader() has been deprecated since .NET 2.0. Use XmlReader.Create() instead. Commented Jul 23, 2013 at 14:18

2 Answers 2

1

Use the XmlDocument class instead of the XmlTextReader class.

System.Xml.XmlDocument xd = new XmlDocument();
xd.Load(@"filepath");

foreach(XmlNode nameNode in xd.GetElementsByTagName("name"))
{
    if(nameNode.ParentNode.Name == "type1")
    {
        string currentNameValue = nameNode.InnerText;
        int cnvLen = currentNameValue.Length;
        string cnvWOdate = currentNameValue.Substring(0,cnvLen-8);
        string newNameValue = cnvWOdate+currTimeDate;

        nameNode.InnerText = newNameValue;
    }
}

xd.Save(@"newFilePath");
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for Quick response. But for some reason (as I struggled earlier), this code is not recognizing "name" element. I have received "Object reference not set to an instance of an object" error message.
Sounds like a NullReferenceException. Which line is this happening on?
I believe this is happening right after currentNameValue = nameNode.InnerText;
HI Nicholas, Just to remind you there are two tags with same name as "name", is this causing problem ?
I have made a few corrections and have tested this code. Apart from not having the string currTimeDate defined in my version, it runs fine and performs as expected.
|
0
XmlDocument doc = new XmlDocument();
doc.Load("file path");

XmlNode nameNode = doc.SelectSingleNode("/Envelope/Body/ca/type1/name");

string currentNameValue = nameNode != null ? nameNode.InnerText : "name not exist";
int cnvLen = currentNameValue.Length;
string cnvWOdate = currentNameValue.Substring(0, cnvLen-8);
string newNameValue = cnvWOdate+currTimeDate;

nameNode.InnerText = newNameValue; //set new value to tag

To get Value or InnerText of a node, you will have to make sure the node exist. The line string currentNameValue has the format like this:

var variable = condition ? A : B;

It's basically saying that if condition is true, then variable equals A, otherwise, variable equals B.

3 Comments

Performing the null check is a good approach if you are calling SelectSingleNode. However, I disagree with your use of a default value "name not exist" because the rest of the code will continue to execute and will produce odd results. It is probably better to wrap the InnerText modification in an if block. My answer avoids this since the foreach loop will only iterate when the node is not null anyway.
@nicholas right, the foreach loop will only iterate the ones that are not null. However, he said that he is still getting null reference, so I suspect that the null reference is from somewhere else, that why I suggest checking the specific ones. And with the default value, "name not exist" is just a suggestion, he can even assign as empty string and check later. But yes, I agree there are multiple ways to attack this problem.
@sora0419 - Thanks for your answer, I have found why I am getting null reference as I given as I have given wrong format in filepath. For my problem, I can vote your answer as acceptable too; unfortunately, this forum gives me option to choose only one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.