Reputation: 24602
I have the following:
var topic = "<option value=\"123\">X</option>\r\n <option value=\"456\">XX</option>\r\n";
And a string that looks like this:
var topicValue = "456";
Is there a way that I can create a new topic string that looks like the following when the topicValue matches:
var topic = "<option value=\"123\">X</option>\r\n <option value=\"456\" selected=\"selected\" >XX</option>\r\n";
What I am not sure of is how to create a regular expression that will work with the backslashes that are in my topic string. Also is there an easier way to do this than using a regulare expression?
Upvotes: 2
Views: 148
Reputation: 3439
It's never a good idea to manipulate an XML as a string. it looks like you have a part of an XML in topic
. Why don't you treat it like an XML. First let's make it a valid XML, just to work with.
<options>
<option value=\"123\" isSelectd=\"false\">X</option>
<option value=\"456\" isSelectd=\"false\">XX</option>
</options>
Root node <options>
is added here and also a new attribute isSelectd
which is initially false for both child nodes. The node have value of 456
should be searched and updated. Now using the XmlDocument class you can easily achieve what you want like this.
var topic = "<options>" +
"<option value=\"123\" isSelectd=\"false\">X</option>" +
"<option value=\"456\" isSelectd=\"false\">XX</option>" +
"</options>";
int selectedValue = 456;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(topic);
foreach (XmlNode node in xmlDoc.ChildNodes[0].ChildNodes)
{
int value = Convert.ToInt32(node.Attributes[0].Value.ToString());
if (value == selectedValue)
node.Attributes[1].Value = "ture";
}
topic = xmlDoc.InnerXml;
topic now contains the following XML,
<options>
<option value=\"123\" isSelectd=\"false\">X</option>
<option value=\"456\" isSelectd=\"true\">XX</option>
</options>
Upvotes: 3
Reputation: 4965
Okay, here is the code. Note that this only works if the whitespace and formatting is consistent:
var topic = // topic here
const string optionFormat = @"<option value=\""{0}\""";
var topicValue = // topic value here
var topicToReplace = String.Format(optionFormat, topicValue);
var replaced = topic.Replace(topicToReplace , topicToReplace + @" selected=\""selected\""");
Upvotes: 1
Reputation: 8706
Write it as just how it is was written. For example:
string myString = "My name is \"John\"";
Console.WriteLine(myString);
myString = myString.Replace("\"John\"", "\"Jony\"");
Console.WriteLine(myString);
Console.ReadLine();
Try code below to achive what you want:
string topic = "<option value=\"123\">X</option>\r\n <option value=\"456\">XX</option>\r\n";
Console.WriteLine(topic);
string topicValue = "456";
string mustBeReplaced = string.Empty;
string replaceResult = string.Empty;
if (topic.Contains(topicValue))
{
mustBeReplaced = "value=\"" + topicValue + "\"";
replaceResult = mustBeReplaced + " selected=\"selected\"";
topic = topic.Replace(mustBeReplaced, replaceResult);
}
Console.WriteLine(topic);
Console.ReadLine();
Upvotes: 1