2

I want XDocument to output the XML prolog (for example "<?xml version="1.0" encoding="UTF-8"?>") in uppercase.

Here is how I'm currently doing it, but that doesn't seem to work:

XDocument doc = new XDocument
(
     new XDeclaration("1.0", "UTF-8",""),
     bla bla bla);
     doc.Save(@"Z:\test.xml");
)

This code isn't working it's coming out in lower case. While doing this the iden't and formatting shouldn't be altererd. Any help would be greatly appreciated. Thanks.

*EDIT:*This question is still open, are there any more ideas to resolve this.

user726720
  • 1,127
  • 7
  • 25
  • 59
  • Oh, the legacies. Please entertain us by telling why you need this? – CodeCaster May 23 '13 at 06:27
  • @CodeCaster, because the XML specification while not enforcing this to be case sensitive, the recommendation is to use UPPERCASE. So the fact that the XDocument class is not respecting this is not a reason to blame something you call *legacies* which is fully W3C compliant and expects uppercase. – Darin Dimitrov May 23 '13 at 06:29
  • 2
    Possible duplicate: http://stackoverflow.com/questions/8547675/get-utf-8-in-uppercase-using-xdocument (Are you working for the same customer?) – Onots May 23 '13 at 06:29
  • I know things work without this, and the xml parser shouldn't be an issue, but doing it is part of the deal. Our old document contains the upper case, so we need to produce the new documents which are consistent for formatting. – user726720 May 23 '13 at 06:31
  • @Onots: no I have tried that solution, it looses the formatting. It doesn't comeout as idented. – user726720 May 23 '13 at 06:32
  • I need to turn this to upper case but my main concern also is to preserve the ident and formatting. Please advise – user726720 May 23 '13 at 06:34
  • Hey maybe this will help: http://stackoverflow.com/questions/3288302/how-can-i-force-xdocument-to-output-utf-8-in-the-declaration-line – Dimitar Dimitrov May 23 '13 at 06:47
  • On the XmlTextWriter you can set the Formatting to be indented. It might help you. I don't have time or desire to write a complete answer right now. – Onots May 23 '13 at 06:49
  • @Dimitar Dimitrov: I have tried that as in my question above, it doesn't help – user726720 May 23 '13 at 06:53
  • @DarinDimitrov I read it like OP wants the entire document in uppercase UTF-8. It seems he just wants the preamble to be uppercase. – CodeCaster May 23 '13 at 06:59
  • @user726720 I edited your question to better explain what you are trying to do. If this is incorrect, please correct it. – CodeCaster May 23 '13 at 07:27
  • @CodeCaster: it's fine, thank you. – user726720 May 23 '13 at 07:33

4 Answers4

2
XDocument xdoc = new XDocument();

.... // do your stuff here

string finalDoc = xdoc.ToString();
string header = finalDoc.Substring(0,finalDoc.IndexOf("?>") + 2); // end of header tag

finalDoc = finalDoc.Replace(header, header.ToUpper());  // replace header with the uppercase version

.... // do stuff with the xml with the upper case header

EDIT:

Oh you only want the UTF-8 upper case?

Then this is more correct:

XDocument xdoc = new XDocument();

.... // do your stuff here

string finalDoc = xdoc.ToString();
string header = finalDoc.Substring(0,finalDoc.IndexOf("?>") + 2); // end of header tag
string encoding = header.Substring(header.IndexOf("encoding=") + 10);
encoding = encoding.Substring(0,encoding.IndexOf("\""); // only get encoding content

// replace encoding with the uppercase version in header, then replace old header with new one.
finalDoc = finalDoc.Replace(header, header.Replace(encoding, encoding.ToUpper()));

.... // do stuff with the xml with the upper case header

This will only replace whatever is in the encoding to uppercase manually.

Sellorio
  • 1,806
  • 1
  • 16
  • 32
  • Thanks, I give you a +1 for this, but this would be my last option to do it manually (I did think of this before). But I want to save some manual job here and trying to learn how can we do this with XDocument. – user726720 May 24 '13 at 05:48
0

Save to a custom stream that modify the header and then pass it on to a FileStream.

KristoferA
  • 12,287
  • 1
  • 40
  • 62
0

Here is a sample solution using XmlTextWriter

I am sure there should be a better optimal way..

            XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8","d"));
            doc.Add(new XElement("MyRoot", "Value blah"));


            using (var str = new StringWriter())
            using (var xmlTextWriter = new XmlTextWriter(str))
            {
                xmlTextWriter.Formatting = Formatting.Indented;
                xmlTextWriter.Indentation = 4;

                xmlTextWriter.WriteRaw(doc.Declaration.ToString().ToUpper());

                foreach (var xElement in doc.Elements())
                {
                   xmlTextWriter.WriteElementString(xElement.Name.ToString(), xElement.Value);
                }

                var finalOutput = str.ToString();
            }

FinalOutput would contain:

<?XML VERSION="1.0" ENCODING="UTF-8" STANDALONE="D"?>
<MyRoot>Value blah</MyRoot>
jacob aloysious
  • 2,547
  • 15
  • 16
0

I had the same problem, the program that should read the XML could not handle the "utf" in lower case.

Found this simple workaround:

    XmlWriterSettings settings = new XmlWriterSettings();

    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    settings.NewLineHandling = NewLineHandling.Replace;
    settings.NewLineOnAttributes = true;       

                using (
                    XmlWriter xmlWriter =
                        XmlWriter.Create(
                            Application.StartupPath + @"\Output\products.xml", settings))
                {

                    xmlWriter.WriteStartDocument();
                    xmlWriter.WriteRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
                    xmlWriter.WriteStartElement("products");

etc....

Rado
  • 151
  • 2
  • 13