简体   繁体   中英

C#: How to remove a line in XML file?

May be it is a silly question, but I need to remove the top line in my XML file. This is the line:

<?xml version="1.0" encoding="utf-16"?>

When doing this

string xmlToSend = ClassToXML(myObject);

string newString= xmlToSend.Replace("<?xml version=\"1.0\" encoding=\"utf - 16\"?>", string.Empty);

nothing happens.

The file still has the line there.

I have a method that serializes my object into an XML string and return that string. How do I remove XML declaration from that string?

private string ClassToXML(Object classObject)
{
    var myString = new System.IO.StringWriter();
    var serializer = new XmlSerializer(classObject.GetType());
    System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, new System.Xml.XmlWriterSettings()
    {
        OmitXmlDeclaration = true,
        ConformanceLevel = System.Xml.ConformanceLevel.Auto,
        Indent = true
    });

    //serializer.Serialize(myString, classObject);
    serializer.Serialize(xw, classObject);

    return myString.ToString();

}

What am I missing?

Your replace has a couple of extra spaces in it which is why its not doing anything, I think this should work instead:

string newString = xmlToSend.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", string.Empty);

But you probably shouldn't be doing this as its fragile and would break if there are any differences in the declaration, eg the following are all valid XML declarations:

<?xml version="1.0"  encoding="utf-16"?>
<?xml version="1.0" encoding="utf-16" ?>
<?xml encoding="utf-16" version="1.0" ?>
<?xml version="1.0" encoding="utf-8"?>

Instead you should use an XML parser that understands how to parse the declaration, and then write a fragment without it, for example:

var doc = XDocument.Load("ConsoleApp1.exe.config");
var settings = new XmlWriterSettings
{
    OmitXmlDeclaration = true
};
var memoryStream = new MemoryStream();
using (var writer = XmlWriter.Create(memoryStream, settings))
{
    doc.WriteTo(writer);
}
var newString = Encoding.UTF8.GetString(memoryStream.ToArray());

In your example you have a method ClassToXML - chances are you could do this inside that method.

Replace() doesn't change the original string, rather output the result to a new string.

Use this instead:

xmlToSend = xmlToSend.Replace("<?xml version=\"1.0\" encoding=\"utf - 16\"?>", string.Empty);

This is a strict answer to your question, but when handling XML you should really use Xml manipulation classes.

The spacing is different from the looks of it. You can start with a simpler check of whether it's finding your string at all with

xmlToSend.IndexOf("<?xml version=\\"1.0\\" encoding=\\"utf-16\\"?>")

Ideally, you could handle this through some C# code that recognizes XML instead of specific strings. There's a question here about omitting the xml declaration that may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM