How to force XDocument to output the prolog in uppercase while preserving identation and formatting?

前端 未结 4 1857
刺人心
刺人心 2021-01-23 05:34

I want XDocument to output the XML prolog (for example \"\") in uppercase.

Here is how I\'m cur

相关标签:
4条回答
  • 2021-01-23 06:11

    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....

    0 讨论(0)
  • 2021-01-23 06:14

    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>
    
    0 讨论(0)
  • 2021-01-23 06:24
    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.

    0 讨论(0)
  • 2021-01-23 06:28

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

    0 讨论(0)
提交回复
热议问题