2012-10-30 2 views
3

XMLSerialize를 사용하여 두 개의 타사 서비스 참조의 멤버가 포함 된 내 클래스 중 하나에서 .xml을 생성하려고합니다.두 서비스 참조에서 같은 클래스를 직렬화

XmlSerializer에서이 오류가 발생했습니다 (두 타사 서비스의 참조에 동일한 클래스 이름이 있기 때문에).

유형 'ExternalServiceReference1.SameClass'와 'ExternalServiceReference2.SameClass은'모두 네임 스페이스에서, ': // blablabla/HTTP'를 XML 형식 이름, 'SameClass'를 사용합니다. XML 속성을 사용하여 유형에 고유 한 XML 이름 및/또는 네임 스페이스를 지정하십시오. ExternalServiceReference2도

내 클래스 보면 뭔가 같이 SameClass 유형의 멤버를 포함에서

는 ExternalServiceReference1에서 TestClass1 유형 SameClass TestClass2의 멤버가 포함

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를 변경하고 싶지 않습니다.

+0

[스레드 (따른 http://stackoverflow.com/questions/4967941/c-sharp-xml-serializable-error-2-types-both- use-the-xml-type-name-relatio)를 사용하려면 클래스에'XmlRoot' 속성을 추가해야합니다. 그것을 속성에 추가 할 수 있습니까? XmlRoot [here] (http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlroot.aspx)에 대한 자세한 정보를 찾을 수 있습니다. – Default

+0

마지막 수단으로 사용해보십시오. var json = JsonConvert.SerializeObject (foo); var xDoc = JsonConvert.DeserializeXNode (json, "Foo"); –

+0

기본값 : XmlRootAttribute는 클래스, 구조체, 열거 형 또는 인터페이스에만 적용 할 수 있습니다. 내 경우 엔 재산이야. – Hockeymtl

답변

0

귀하의 어려움을 이해했다면 도움이 될 것입니다.

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> 
관련 문제