0

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?

gene
  • 2,098
  • 7
  • 40
  • 98

3 Answers3

4

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, e.g. 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.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • But how would I return xml string from the method? Currently my method serializes my object to a string and returns string. – gene Aug 08 '17 at 18:59
  • I have posted my method inside of my question – gene Aug 08 '17 at 19:07
0

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.

Sach
  • 10,091
  • 8
  • 47
  • 84
0

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.

Graham
  • 336
  • 1
  • 13