Question

the c# code as below:

using (XmlReader Reader = XmlReader.Create(DocumentPath, XmlSettings))
{
    while (Reader.Read())
    {
        switch (Reader.NodeType)
        {
            case XmlNodeType.Element:
                //doing function
                break;
            case XmlNodeType.Text:
                if(Reader.Value.StartsWith("_"))
                {
                     // I want to replace the Reader.Value to new string
                }
                break;
            case XmlNodeType.EndElement:
                //doing function
                break;
            default:
                //doing function
                break;
        }
    }
}

I want to set the new value when XmlNodeType = text.

Was it helpful?

Solution

There are 3 steps to this operations:

  1. Load the document as an object model(XML is a hierarchy, if that makes it easier)
  2. Alter the value in the object model
  3. Resave the model as a file

The method of loading is up to you but i would recommend using XDocument and the related Linq-to-XML classes for smaller tasks like this. It is crazy easy as demonstrated in this stack.

Edit - a useful quote for your scenario

XML elements can contain text content. Sometimes the content is simple (the element only contains text content), and sometimes the content is mixed (the contents of the element contains both text and other elements). In either case, each chunk of text is represented as an XText node.

From MSDN - XText class

Here is a code sample for a console application using the xml from the comments:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace TextNodeChange
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = @"<Info><title>a</title><content>texttexttexttexttext<tag>TAGNAME</tag>texttexttex‌​ttexttext</content>texttexttexttexttext</Info>";

            XDocument doc = XDocument.Parse(input);
            Console.WriteLine("Input document");
            Console.WriteLine(doc);

            //get the all of the text nodes in the content element
            var textNodes = doc.Element("Info").Element("content").Nodes().OfType<XText>().ToList();

            //update the second text node
            textNodes[1].Value = "THIS IS AN ALTERED VALUE!!!!";

            Console.WriteLine();
            Console.WriteLine("Output document");
            Console.WriteLine(doc);
            Console.ReadLine();
        }
    }
}

Why are 3 steps required?

Elements of xml are of variable length in a file. Altering a value may change the length of that part of the file and overwrite other parts. Therefore, you have to deserialize the whole document, make the changes and save it again.

OTHER TIPS

You cant replace the reader.value readonly property. You need to use XmlWriter to create your own xml and replace any value you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top