2011-06-10 67 views
2

我正在嘗試讀取一堆xsd文件和編譯XmlSchemaSet中的架構的代碼。XmlSchema刪除重複類型

問題是這些xsd文件來自各種來源,它們可能有多次聲明的元素/類型,我應該刪除它,否則我會編譯XmlSchemaSet的方法會引發錯誤。

有沒有推薦的做這種類型的事情?

回答

1

我跟着從這個MSDN後的步驟和它的工作對我來說:

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/7f1b7307-98c8-4457-b02b-1e6fa2c63719/

的基本思想是在新的架構經過類型,如果他們的存在,從該模式中刪除現有的模式。

class Program 
    { 
    static void Main(string[] args) 
    { 
     XmlSchemaSet schemaSet = MergeSchemas(@"..\..\XMLSchema1.xsd", @"..\..\XMLSchema2.xsd"); 
     foreach (XmlSchema schema in schemaSet.Schemas()) 
     { 
     schema.Write(Console.Out); 
     Console.WriteLine(); 
     } 
    } 

    public static XmlSchemaSet MergeSchemas(string schema1, string schema2) 
    { 
     XmlSchemaSet schemaSet1 = new XmlSchemaSet(); 
     schemaSet1.Add(null, schema1); 
     schemaSet1.Compile(); 

     XmlSchemaSet schemaSet2 = new XmlSchemaSet(); 
     schemaSet2.Add(null, schema2); 
     schemaSet2.Compile(); 

     foreach (XmlSchemaElement el1 in schemaSet1.GlobalElements.Values) 
     { 
     foreach (XmlSchemaElement el2 in schemaSet2.GlobalElements.Values) 
     { 
      if (el2.QualifiedName.Equals(el1.QualifiedName)) 
      { 
      ((XmlSchema)el2.Parent).Items.Remove(el2); 
      break; 
      } 
     } 
     } 
     foreach (XmlSchema schema in schemaSet2.Schemas()) 
     { 
     schemaSet2.Reprocess(schema); 
     } 
     schemaSet2.Compile(); 
     schemaSet1.Add(schemaSet2); 

     return schemaSet1; 
    } 
    } 
+0

其同樣的問題問我:) – 2011-10-26 01:35:08

+0

哈哈,這很有趣。這是一個非常尷尬的API,重新處理模式和重新編譯的所有步驟都完全違反直覺。 – 2011-10-26 21:52:14