2

我想XDocument以大写形式输出 XML 序言(例如“ <?xml version="1.0" encoding="UTF-8"?>”)。

这是我目前的做法,但这似乎不起作用:

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

此代码不起作用,它以小写形式出现。在执行此操作时,不应更改身份和格式。任何帮助将不胜感激。谢谢。

*编辑: *这个问题仍然悬而未决,是否有更多的想法来解决这个问题。

4

4 回答 4

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

编辑:

哦,你只想要 UTF-8 大写?

那么这更正确:

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

这只会手动将编码中的任何内容替换为大写。

于 2013-05-24T04:10:30.883 回答
0

保存到修改标头的自定义流,然后将其传递给 FileStream。

于 2013-05-23T07:20:24.403 回答
0

这是使用XmlTextWriter的示例解决方案

我相信应该有更好的最佳方式..

            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将包含:

<?XML VERSION="1.0" ENCODING="UTF-8" STANDALONE="D"?>
<MyRoot>Value blah</MyRoot>
于 2013-05-24T04:17:20.350 回答
0

我遇到了同样的问题,应该读取 XML 的程序无法处理小写的“utf”。

找到了这个简单的解决方法:

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

于 2015-09-22T14:21:01.490 回答