如何强制的XDocument输出序言大写,而preserving identation和格式? [英] How to force XDocument to output the prolog in uppercase while preserving identation and formatting?

查看:438
本文介绍了如何强制的XDocument输出序言大写,而preserving identation和格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的XDocument 输出XML序言(例如,< XML版本=1.0编码=UTF-8 ?> ),大写

下面是如何我目前正在做它,但似乎并没有工作:

 的XDocument DOC =新的XDocument
(
     新XDeclaration(1.0,UTF-8,),
     唧唧歪歪);
     doc.Save(@Z:\的test.xml);
)
 

这code不工作它的未来在较低的情况下。而这样做的iden't和格式不应该是altererd。 任何帮助将大大AP preciated。谢谢你。

*的编辑:的*这个问题仍然是开放的,有什么更多的想法来解决这个

解决方案

  XDOC的XDocument =新的XDocument();

.... //做你的东西在这里

字符串finalDoc = xdoc.ToString();
串标头= finalDoc.Substring(0,finalDoc.IndexOf(>?)+ 2); //头标记的结束

finalDoc = finalDoc.Replace(首标,header.ToUpper()); //替换头用大写版本

.... //做的东西与上壳头的XML
 

编辑:

哦,你只需要使用UTF-8大写?

然后,这是比较正确的:

  XDOC的XDocument =新的XDocument();

.... //做你的东西在这里

字符串finalDoc = xdoc.ToString();
串标头= finalDoc.Substring(0,finalDoc.IndexOf(>?)+ 2); //头标记的结束
字符串编码= header.Substring(header.IndexOf(编码=)+ 10);
编码= encoding.Substring(0,encoding.IndexOf(\); //只能获得编码的内容

//替换编码的大写版本的标题,然后更换新的旧的头。
finalDoc = finalDoc.Replace(首标,header.Replace(编码,encoding.ToUpper()));

.... //做的东西与上壳头的XML
 

这将只替换无论是在编码手动大写。

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.

解决方案

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.

这篇关于如何强制的XDocument输出序言大写,而preserving identation和格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆