XmlSerializer serializer = new XmlSerializer(typeof(IxComment));
System.IO.StringWriter aStream = new System.IO.StringWriter();
serializer.Serialize(aStream,Comments);
commentsString = aStream.ToString();
这里的 commentsString 中有以下元素
<IxComment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
是否有可能互换 xsi 和 xsd 属性并获得如下所示的元素
<IxComment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
这会导致任何其他问题吗?
编辑:为什么我需要这样做?
我们正在将现有应用程序从 1.1 迁移到 3.0,代码中有一个 if 循环
int iStartTagIndex = strXMLString.IndexOf("<IxComment xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
检查 IxComment 的索引。 这里串行器的 o/p 和条件在 xsd 和 xsi 的位置不同。所以我想知道我们是否可以指示序列化程序根据需要提供 o/p。
我在这里还有一个问题,因为这是一个现有的应用程序,序列化器 O/P 是否因版本而异?
最佳答案
我希望没有办法影响那些对任何理解 XML 的代码都无关紧要的事情的顺序。任何与 namespace 声明顺序有问题的代码都严重损坏,必须修复,句号。
看到您的修改后,我更加坚定:修复您损坏的代码。您的代码不应该对 XML 执行字符串处理。您应该简单地修复您的代码,而不是尝试修复 XML 标准,该标准规定命名空间声明的顺序不相关。
关于c# - 在 XmlSerializer 的输出中交换 xsd 和 xsi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577895/