I have a text file:
I put this piece of code in a string, and I need to replace the name inside (in the Mateus case) with another name of another string.
I have a text file:
I put this piece of code in a string, and I need to replace the name inside (in the Mateus case) with another name of another string.
You can use the String.Replace.
Ex:
public static void Main()
{
String s = new String('a', 3);
Console.WriteLine("The initial string: '{0}'", s);
s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
Console.WriteLine("The final string: '{0}'", s);
}
Reference: link
What I understood from your question was:
You want to change what's inside the <name>..esse texto aqui..</name>
being that this text belongs to a String
Then do the following:
// adicione a referência
using System.Xml;
//carregando o texto que você mencionou em uma string
String textoXml = "<Farmer><name>nome</name><isEmoting>false</isEmoting><isCharging>false</isCharging></Farmer>";
// Criando um novo xmlDocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = textoXml;// pasando para o Xml o conteúdo da string
//Pegando o elemento pelo nome da TAG
XmlNodeList xNodeList = xmlDoc.GetElementsByTagName("name");
foreach (XmlNode xNode in xNodeList)
{
xNode["name"].InnerText = "outroNome";// alterando o texto desejado
}
String textoAlterado = xmlDoc.InnerXml; //texto alterado
The result will be:
<Farmer><name>outroNome</name><isEmoting>false</isEmoting><isCharging>false</isCharging></Farmer>