Current Implementation
Sql Server 2005 Database with a table called messages with a column called MessageXml of type xml.
Sql Server 2005数据库,带有一个名为messages的表,其列名为MessageXml,类型为xml。
C# Library project with a Linq to Sql class that generates a class called Message with a field called MessageXml of type XElement.
带有Linq to Sql类的C#Library项目,它生成一个名为Message的类,其字段名为MessageXml,类型为XElement。
WCF Webservice that exposes a MessagePayload class with a property called MessageXml of type XElement.
WCF Webservice使用类型为XElement的MessageXml属性公开MessagePayload类。
The webservice doesn't serve up my Message class created by Linq to Sql. I use a light weight object as a go between.
Web服务不提供由Linq创建的Message类到Sql。我使用轻量级物体作为介于两者之间。
Question
Is XElement really the type I want to be using in my WCF Service or is there a better type. The xml that is intended to be passed into the service should be a full doc. Also, I'm having a bit of trouble loading xml documents as an XElement. I think that I should expose a full xml document type in the light weight class for the service but I'm a bit confused on the differences between XDocument and XmlDocument.
XElement是否真的是我想在我的WCF服务中使用的类型,或者是更好的类型。要传递给服务的xml应该是完整的doc。另外,我在将xml文档作为XElement加载时遇到了一些麻烦。我认为我应该在服务的轻量级中公开一个完整的xml文档类型,但我对XDocument和XmlDocument之间的差异感到有点困惑。
On top of that, I can't expose the WCF Message class with a property of XDocument type because it contains a property of XDeclaration type that can't be serialized.
最重要的是,我不能使用XDocument类型的属性公开WCF Message类,因为它包含无法序列化的XDeclaration类型的属性。
If I use XmlDocument than I have to do this weird conversion of xml types in my translation between the Linq class and the lightweight class.
如果我使用XmlDocument而不是我必须在Linq类和轻量级类之间的翻译中进行奇怪的xml类型转换。
XmlDocument doc = new XmlDocument();
doc.LoadXml(message.MessageXml.ToString());
MessageEnvelope retVal = new MessageEnvelope()
{
MessageXml = doc,
};
XmlDocument seems like the right one and I know I'll have to do some translation but want to get as close to appropriate as possible.
XmlDocument看起来是正确的,我知道我将不得不做一些翻译,但希望尽可能接近合适。
4 个解决方案
#1
You can use either XElement or XmlElement:
您可以使用XElement或XmlElement:
public XmlElement GetXmlElement()
{
var doc = new XmlDocument();
doc.Load(PREFIX + @"Enumerations.wsdl");
return doc.DocumentElement;
}
public XElement GetXElement()
{
var doc = XDocument.Load(PREFIX + @"Enumerations.wsdl");
return doc.Root;
}
You do not want either XDocument
nor XmlDocument
. Remember that whatever you return will be in the middle of an XML document containing the SOAP envelope. You can't have a document inside a document, so what you want is an element.
您既不需要XDocument也不需要XmlDocument。请记住,无论您返回什么,都将位于包含SOAP信封的XML文档的中间。您不能在文档中包含文档,因此您需要的是一个元素。
#2
John Saunders is on the money here. To elaborate slightly, if you look at the WSDL that is generated when you return either an XmlElement
or an XElement
, you'll see something like this:
约翰桑德斯在这里有钱。稍微详细说一下,如果你看一下返回XmlElement或XElement时生成的WSDL,你会看到类似这样的东西:
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" processContents="lax"/>
</xs:sequence>
</xs:complexType>
That's it. It basically means any XML can go here. It also means that, contrary to Chansik's suggestion, it does not bind the return type to a .NET specific type.
而已。它基本上意味着任何XML都可以在这里。这也意味着,与Chansik的建议相反,它不会将返回类型绑定到.NET特定类型。
So yeah, you don't need to use a string.
所以是的,你不需要使用字符串。
#3
Use whatever type you need to serialize the class (String has always worked well for me) and then do conversions when necessary on server or client side to maintain the integrity of the document. You can build an XDocument out of one or more XElements also, so I would go with XElement.
使用序列化类所需的任何类型(String对我来说一直很好),然后在服务器或客户端进行必要的转换以维护文档的完整性。你也可以用一个或多个XElements构建一个XDocument,所以我会使用XElement。
Worst case, you can even override the serialization behavior of your class (not too terrible if you're good with reflection).
最糟糕的情况是,你甚至可以覆盖你班级的序列化行为(如果你善于反思,那就太糟糕了)。
XmlDocument is the old, non Linq document. They're the same concept implemented in different classes.
XmlDocument是旧的非Linq文档。它们是在不同类中实现的相同概念。
#4
In general, XDocument
and XElememnt
are preferable to XmlDocument
and XmlElement
in terms of performance.
通常,XDocument和XElememnt在性能方面优于XmlDocument和XmlElement。
However, I would suggest to use string
to send an XML document via WCF service for the following reasons:
但是,我建议使用字符串通过WCF服务发送XML文档,原因如下:
-
Interoperability
- Clients are not tied to specific .NET framework version (Client can choose to use
XDocument
orXmlDocument
. Even Java-based clients can be supported as long as WCF services are configured in such way).
客户端不依赖于特定的.NET框架版本(客户端可以选择使用XDocument或XmlDocument。只要以这种方式配置WCF服务,甚至可以支持基于Java的客户端)。
- Clients are not tied to specific .NET framework version (Client can choose to use
- Handling Xml declaration properly if the original XML document contains it.
互操作性客户端与特定的.NET框架版本无关(客户端可以选择使用XDocument或XmlDocument。只要以这种方式配置WCF服务,甚至可以支持基于Java的客户端)。
如果原始XML文档包含Xml声明,则正确处理Xml声明。
Note: Please make sure to adjust the configuration appropriately to support a large xml document. For example, basicHttpBinding
's default max message size is 64KB.
注意:请确保适当调整配置以支持大型xml文档。例如,basicHttpBinding的默认最大邮件大小为64KB。
#1
You can use either XElement or XmlElement:
您可以使用XElement或XmlElement:
public XmlElement GetXmlElement()
{
var doc = new XmlDocument();
doc.Load(PREFIX + @"Enumerations.wsdl");
return doc.DocumentElement;
}
public XElement GetXElement()
{
var doc = XDocument.Load(PREFIX + @"Enumerations.wsdl");
return doc.Root;
}
You do not want either XDocument
nor XmlDocument
. Remember that whatever you return will be in the middle of an XML document containing the SOAP envelope. You can't have a document inside a document, so what you want is an element.
您既不需要XDocument也不需要XmlDocument。请记住,无论您返回什么,都将位于包含SOAP信封的XML文档的中间。您不能在文档中包含文档,因此您需要的是一个元素。
#2
John Saunders is on the money here. To elaborate slightly, if you look at the WSDL that is generated when you return either an XmlElement
or an XElement
, you'll see something like this:
约翰桑德斯在这里有钱。稍微详细说一下,如果你看一下返回XmlElement或XElement时生成的WSDL,你会看到类似这样的东西:
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" processContents="lax"/>
</xs:sequence>
</xs:complexType>
That's it. It basically means any XML can go here. It also means that, contrary to Chansik's suggestion, it does not bind the return type to a .NET specific type.
而已。它基本上意味着任何XML都可以在这里。这也意味着,与Chansik的建议相反,它不会将返回类型绑定到.NET特定类型。
So yeah, you don't need to use a string.
所以是的,你不需要使用字符串。
#3
Use whatever type you need to serialize the class (String has always worked well for me) and then do conversions when necessary on server or client side to maintain the integrity of the document. You can build an XDocument out of one or more XElements also, so I would go with XElement.
使用序列化类所需的任何类型(String对我来说一直很好),然后在服务器或客户端进行必要的转换以维护文档的完整性。你也可以用一个或多个XElements构建一个XDocument,所以我会使用XElement。
Worst case, you can even override the serialization behavior of your class (not too terrible if you're good with reflection).
最糟糕的情况是,你甚至可以覆盖你班级的序列化行为(如果你善于反思,那就太糟糕了)。
XmlDocument is the old, non Linq document. They're the same concept implemented in different classes.
XmlDocument是旧的非Linq文档。它们是在不同类中实现的相同概念。
#4
In general, XDocument
and XElememnt
are preferable to XmlDocument
and XmlElement
in terms of performance.
通常,XDocument和XElememnt在性能方面优于XmlDocument和XmlElement。
However, I would suggest to use string
to send an XML document via WCF service for the following reasons:
但是,我建议使用字符串通过WCF服务发送XML文档,原因如下:
-
Interoperability
- Clients are not tied to specific .NET framework version (Client can choose to use
XDocument
orXmlDocument
. Even Java-based clients can be supported as long as WCF services are configured in such way).
客户端不依赖于特定的.NET框架版本(客户端可以选择使用XDocument或XmlDocument。只要以这种方式配置WCF服务,甚至可以支持基于Java的客户端)。
- Clients are not tied to specific .NET framework version (Client can choose to use
- Handling Xml declaration properly if the original XML document contains it.
互操作性客户端与特定的.NET框架版本无关(客户端可以选择使用XDocument或XmlDocument。只要以这种方式配置WCF服务,甚至可以支持基于Java的客户端)。
如果原始XML文档包含Xml声明,则正确处理Xml声明。
Note: Please make sure to adjust the configuration appropriately to support a large xml document. For example, basicHttpBinding
's default max message size is 64KB.
注意:请确保适当调整配置以支持大型xml文档。例如,basicHttpBinding的默认最大邮件大小为64KB。