.NET XmlSerializer 到元素 FormDefault=Unqualified XML? [英] .NET XmlSerializer to Element FormDefault=Unqualified XML?
问题描述
我或多或少像这样使用 C# 代码将对象序列化为 XML:
I use C# code more-or-less like this to serialize an object to XML:
XmlSerializer xs1 = new XmlSerializer(typeof(YourClassName));
StreamWriter sw1 = new StreamWriter(@"c:\DeserializeYourObject.xml");
xs1.Serialize(sw1, objYourObjectFromYourClassName);
sw1.Close();
我希望它像这样序列化:
I want it to serialize like this:
<ns0:Header xmlns:ns0="https://mynamespace/">
<SchemaVersion>1.09</SchemaVersion>
<DateTime>2009-12-15T00:00:01-08:00</DateTime>
相反,它是这样做的:
<Header xmlns="https://mynamespace/">
<SchemaVersion xmlns="">V109</SchemaVersion>
<DateTime xmlns="">2010-03-08T18:21:09.100125-08:00</DateTime>
它的序列化方式不适用于我计划使用的 XPath,并且与我的 BizTalk 架构不匹配.最初,我使用 BizTalk 2006 架构中的 XSD.exe 构建了该类,然后将其用作 WCF Web 服务的参数.
The way it is serializing doesn't work with the XPath I had planned to use, and doesn't match my BizTalk schema. Originally I built the class using XSD.exe from a BizTalk 2006 schema, then I use it for an argument to a WCF web service.
这可能与称为元素 FormDefault = Qualified 或 Unqualified 的选项有关.在 BizTalk 中,我将架构设置为Unqualfiied",这正是我想要的.
This might be related to an option called element FormDefault = Qualified or Unqualified. In BizTalk, my I have the schema set to "Unqualfiied" which is what I want.
序列化程序有没有办法输出不合格"的结果?
Is there any way for the serializer to output "unqualified" results?
谢谢,
尼尔·沃尔特斯
更新:
DateTime 上的示例属性:
Sample attribute on DateTime:
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public System.DateTime DateTime
{
get
{
return this.dateTimeField;
}
set
{
this.dateTimeField = value;
}
}
BizTalk 提供了它所谓的提升(或区分)字段,这些字段使用 XPath 提取单个元素的值.我在一个名为 StylusStudio 的工具中检查了 BizTalk 的 XPath,并且 Biztalk'x xpath 不适用于上面的 xmlns='' 字段.
BizTalk provides for what it calls promoted (or distinguished) fields, which use XPath to pull out values of individual elements. I checked the XPath of BizTalk in a tool called StylusStudio, and Biztalk'x xpath didn't work with the xmlns='' fields above.
我的 WCF Web 服务所做的第一件事是将对象序列化为字符串(使用 UTF16 编码)并将其存储在 SQL 数据库的 XML 列中.从那里我看到了上面带有 xmlns="" 的 xml 示例.
The first thing my WCF web service does is to serialize the object to a string (using UTF16 encoding) and store it in an XML column in a SQL database. It is from there I am seeing the above xml sample with the xmlns="".
XPath:
/*[local-name()='Header' and namespace-uri()='https://mynamespace/']/*[local-name()='DateTime' and namespace-uri()='']
推荐答案
我的问题有点混乱,所以这个答案可能对某人有帮助,也可能没有帮助.这是一个相当复杂的场景,我的一半问题来自试图简化它以在此处轻松发布.
My question was a bit muddled, so this answer may or may not help someone. This is a fairly complex scenario, and half of my issues came from trying to simplify it to make an easy post here.
我实际上是使用 C# 例程以编程方式添加一个新元素(请参阅下面的NewElement").C# 代码没有将其命名空间设置为空字符串,因此我相信它继承了Header"元素的命名空间.
I was actually adding a new element programmatically with a C# routine (see "NewElement" below). The C# code did not set its namespace to an empty string, therefore I believe it is inheriting the namespace of the "Header" element.
我有点害怕,因为我得出的结论是 DateTime 不应该有 "xmlns=""',而实际上它应该有.即使 DateTime 属于 Header,它也不会也不应该继承 Header 的命名空间.
I freaked out a little because I was jumping to the conclusion that DateTime should not have the "xmlns=""' when in fact it should. Even though DateTime falls under Header, it does not nor should not inherit the Header's namespace.
在 BizTalk 中,通常只有复杂类型才有自己的命名空间,而 DateTime 和 NewElement 都是简单类型.
In BizTalk, typically only complex types have their own namespace, and DateTime as well as NewElement are simple types.
<Header xmlns="https://mynamespace/">
<SchemaVersion xmlns="">V109</SchemaVersion>
<DateTime xmlns="">2010-03-08T18:21:09.100125-08:00</DateTime>
<NewElement>myvalue</NewElement>
因此,实际上,就 XPath 而言,我最初发布的两个 XML 是相同的.如果我插入一个新元素,我需要确保它遵循相同的模式.
So in effect, the two XML's I posted originally are identical as far as XPath goes. If I insert a new element, I need to make sure it follows the same pattern.
我在一年多前编写了 C# 例程来添加元素,当时它运行良好,所以我不怀疑是它导致了这个问题.
I had written the C# routine to add the element more than a year ago, and it worked fine then, so I wasn't suspect that it was causing this problem.
这篇关于.NET XmlSerializer 到元素 FormDefault=Unqualified XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!