supersophisticated
supersophisticated

Reputation: 127

Replace a string in an XML string

I have a string that looks like this:

newNodeXML = "<item id="qDf73w8emTg" parent_id="weLPzE243de" type="suite">
                 <content>
                    <name>Three</name>
                 </content>
              </item>"

In my [WebMethod], I am trying to replace the parent_ID (randomly generated during run-time) like this:

Regex myRegex = new Regex(@""" parent_id=""(.*?)"" type=""");
newNodeXML = myRegex.Replace(newNodeXML, "d43df2qT45");

Please NOTE that for example/demo sake I have used 'd43df2qT45' in the second line above. I will actually be generating that randomly too.

My problem here is that the result of this comes out to be. I do not want this:

<item id="qDf73w8emTgd43df2qT45suite">
   <content>
      <name>Three</name>
   </content>
</item>

Instead, this is what I want it to be:

<item id="qDf73w8emTg" parent_ID="d43df2qT45" type="suite">
   <content>
      <name>Three</name>
   </content>
</item>

P.S. I have tried some examples/google searches and all I could find were examples that got me this far.

Upvotes: 0

Views: 3950

Answers (5)

Kyle Goode
Kyle Goode

Reputation: 1178

You can use regex for what your wanting to do by using a regex like below.

Regex myRegex = new Regex("parent_id=\"([^""]+)\");
string myXml = "<myxml>data</myxml>";
xdoc.LoadXml(myXml);

Upvotes: 0

Guffa
Guffa

Reputation: 700322

You can use parentheses to catch the part before and after the value, then use $1 and $2 to include them in the replacement:

Regex myRegex = new Regex(@"( parent_id="")[^""]+("")");
newNodeXML = myRegex.Replace(newNodeXML, "$1" + "d43df2qT45" + "$2");

You can also do it using just string operations:

int pos1 = newNodeXML.IndexOf(" parent_id=\"") + 12;
int pos2 = newNodeXML.IndexOf('"', pos1);
newNodeXML = newNodeXML.Substring(0, pos1) + "d43df2qT45" + newNodeXML.Substring(pos2);

Upvotes: -1

Sergio0694
Sergio0694

Reputation: 4567

You can try with this, I tested it and it seems to work:

String newXml = Regex.Replace(xml, "parent_id=\".+\" ", "parent_id=\"" + newID + "\" ");

Here is a sample method to test it:

String xml = "<item id=\"qDf73w8emTg\" parent_id=\"weLPzE243de\" type=\"suite\">\n\t<content>\n\t\t<name>Three</name>\n\t</content>\n</item>";
String newID = "This is the new parent_Id";
Console.WriteLine("Old xml: \n\n" + xml + "\n\n\nNew xml:\n");
String newXml = Regex.Replace(xml, "parent_id=\".+\" ", "parent_id=\"" + newID + "\" ");
Console.WriteLine(newXml);
Console.ReadKey();

Just paste it inside the main method of a console app, and include the RegularExpression library :)

Upvotes: 0

user557597
user557597

Reputation:

Since you've hardcoded everything, there is really no need to use capture group's.
Just extend the replacement to this:

"\" parent_id=\"" + "d43df2qT45" + "\" type=\""

Upvotes: 0

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

If you have a known XML structure, using XML tools is probably better idea than using refex, and faster too. For example:

var doc = XDocument.Parse(newNodeXML);
doc.Root.Attribute("parent_id").Value = "xyz";

This code relies on the exact structure you provided. So there's only one item, it's the root of an XML file and it has an attribute called parent_id.

More about the XDocument type on MSDN.

Upvotes: 6

Related Questions