3

XMLSerialize を使用して、2 つのサードパーティ サービス参照のメンバーを含むクラスの 1 つから .xml を生成しようとしています。

XmlSerializer でこのエラーが発生しました (両方のサードパーティ サービスの参照に同じクラス名があるため)。

タイプ 'ExternalServiceReference1.SameClass' と 'ExternalServiceReference2.SameClass' は両方とも、名前空間 'http://blablabla/' からの XML タイプ名 '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 呼び出しに属性を追加することである場合、問題はありません。しかし、2 つの外部参照用に生成された reference.cs を変更したくありません。

4

1 に答える 1

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>
于 2012-11-15T15:10:08.037 に答える