我正在尝试使用XMLSerialize从我的一个类生成一个.xml,该类包含来自两个第三方Service引用的成员。
我在XmlSerializer上收到此错误(因为两个第三方服务在其引用中都具有相同的类名)。
类型'ExternalServiceReference1.SameClass'和
“ ExternalServiceReference2.SameClass”都使用XML类型名称,
来自命名空间“ http:// blablabla /”的“ SameClass”。使用XML属性
为类型指定唯一的XML名称和/或名称空间。
来自ExternalServiceReference1的TestClass1包含类型SameClass的成员
ExternalServiceReference2中的TestClass2还包含SameClass类型的成员
我的课看起来像这样:
using ExternalServiceReference1; // This is the first thrid-party service reference, that contain the TestClass1.
using ExternalServiceReference2; // This is the second thrid-party service reference, that contain the TestClass2.
[Serializable]
public class Foo
{
public TestClass1 testClass1;
public TestClass2 TestClass2;
}
My test program :
class Program
{
static void Main(string[] args)
{
var xmlSerializer = new XmlSerializer(Foo.GetType());
}
}
我的问题 :
如何在不修改项目中两个服务引用的reference.cs的情况下解决此问题?
如果解决方案是在我自己的类(Foo)或XmlSerializer调用上添加属性,我没有问题。
但是我不想更改为两个外部参考生成的reference.cs。
最佳答案
如果我了解您的难题,这会有所帮助。
namespace XmlSerializerTest
{
class Program
{
static void Main(string[] args)
{
Example exampleClass = new Example();
exampleClass.someClass1 = new ext1.SomeClass(){ Value = "Hello" };
exampleClass.someClass2 = new ext2.SomeClass(){ Value = "World" };
var xmlSerializer = new XmlSerializer(typeof(Example));
xmlSerializer.Serialize(Console.Out, exampleClass);
Console.ReadLine();
}
}
[XmlRoot(ElementName = "RootNode", Namespace = "http://fooooo")]
public class Example
{
[XmlElement(ElementName = "Value1", Type = typeof(ext1.SomeClass), Namespace = "ext1")]
public ext1.SomeClass someClass1 { get; set; }
[XmlElement(ElementName = "Value2", Type = typeof(ext2.SomeClass), Namespace = "ext2")]
public ext2.SomeClass someClass2 { get; set; }
}
}
输出:
<?xml version="1.0" encoding="ibm850"?>
<RootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:
//www.w3.org/2001/XMLSchema" xmlns="http://fooooo">
<Value1 xmlns="ext1">
<Value>Hello</Value>
</Value1>
<Value2 xmlns="ext2">
<Value>World</Value>
</Value2>
</RootNode>
关于c# - 从两个服务引用序列化相同的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13144453/