c# - 属性不包含在生成的代理类中

标签 c# xml wcf xml-serialization

使用 .Net 3.0 和 VS2005。

有问题的对象从 WCF 服务中使用,然后序列化回 XML 以供遗留 API 使用。因此,它不是序列化 TestObject,而是序列化缺少 [XmlRoot] 属性的 .TestObject;但是,子元素的所有 [Xml*] 属性都在生成的代理代码中,因此它们工作得很好。所以所有的子元素都工作得很好,但是封闭元素没有,因为 [XmlRoot] 属性没有包含在生成的代理代码中。包含 [XmlRoot] 属性的原始对象可以手动很好地序列化。

我能否让代理代码包含 [XmlRoot] 属性,以便生成的代理类也能正确序列化?如果我做不到,我怀疑我将不得不使用 [XmlType]但这会造成轻微的破坏,需要我更改其他组件,所以我更喜欢前者。我还想避免手动编辑自动生成的代理类。

这里是一些示例代码(我将客户端和服务包含在同一个应用程序中,因为这样速度快且用于测试目的。注释掉服务引用代码并在运行应用程序时添加服务引用,然后取消注释服务代码并运行。)

namespace SerializationTest {  
  class Program {  
    static void Main( string[] args ) {  

        Type serviceType = typeof( TestService );  
        using (ServiceHost host = new ServiceHost(   
            serviceType,   
            new Uri[] {   
                new Uri( "http://localhost:8080/" )  
            }  

        ))
        {

            ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();  
            behaviour.HttpGetEnabled = true;  
            host.Description.Behaviors.Add( behaviour );  

            host.AddServiceEndpoint( serviceType, new BasicHttpBinding(), "TestService" );  
            host.AddServiceEndpoint( typeof( IMetadataExchange ), new BasicHttpBinding(), "MEX" );  


            host.Open();  

            TestServiceClient client = new TestServiceClient();  
            localhost.TestObject to = client.GetObject();  

            String XmlizedString = null;  
            using (MemoryStream memoryStream = new MemoryStream()) {
                XmlSerializer xs = new XmlSerializer( typeof( localhost.TestObject ) );  
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream)) {
                    xs.Serialize( xmlWriter, to );  
                    memoryStream = (MemoryStream)xmlWriter.BaseStream;  
                    XmlizedString = Encoding.UTF8.GetString( memoryStream.ToArray() );  
                    Console.WriteLine( XmlizedString );  
                }    
            }    
        }

        Console.ReadKey();  
    }  
}  

[Serializable]  
[XmlRoot( "SomethingElse" )]  
public class TestObject {  

    private bool _worked;  

    public TestObject() { Worked = true; }  

    [XmlAttribute( AttributeName = "AttributeWorked" )]  
    public bool Worked {  
        get { return _worked; }  
        set { _worked = value; }  
    }  
}  

[ServiceContract]  
public class TestService {  

    [OperationContract]  
    [XmlSerializerFormat]  
    public TestObject GetObject() {  
        return new TestObject();  
    }  
  }  
}  

这是生成的 Xml。

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" AttributeWorked="true" /> 

最佳答案

== 如果 ==

这仅适用于 XmlRoot 属性。 XmlSerializer 有一个构造函数,您可以在其中指定 XmlRoot 属性。

感谢 csgero 指出它。他的评论应该是解决方案。

XmlSerializer Constructor (Type, XmlRootAttribute)

Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize an XML document into object of the specified type. It also specifies the class to use as the XML root element.

关于c# - 属性不包含在生成的代理类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/159373/

相关文章:

c# - 如何将 HTML 传递给 Razor 局部 View ?

c# - 线程实现 (C#)

xml - XSD:如何根据某些标签的值验证 XML 文件?

java - JUnit:无法加载 ApplicationContext(无合格 Bean)。本地没有问题

xml - Spring 的WebApproot

.net - WCF错误: The caller was not authenticated by the service

c# - WCF 删除 xmlns :tem ="http://tempuri.org/"

c# - WPF 使用命令和事件处理程序

c# 命名空间已经包含继承类的定义,那么如何添加构造函数

c# - 用于在平面文件中对字段进行分组和结构化数据的设计模式