2012-10-18 46 views
0

我需要從數據庫中提取數據並通過ssis創建一個xml文件。到目前爲止,我已經創建了一個腳本,可以成功創建一個xml文件(使用XmlTextWriter),列出了所有必要的數據,但是他們也希望在該文件中列出模式,我不知道該怎麼做。SSIS:將模式添加到xml文件

這裏是我當前的代碼(在我的數據流量使用腳本組件)創建我的XML文件:

​​
public override void PreExecute() 
{ 


    rowName = "Responses"; 
    collectionName = "NewDataSet"; 
    String fileName = Variables.FullFileName;   
    xWriter = new XmlTextWriter(fileName, null); 
    xWriter.WriteStartDocument(); 
    xWriter.WriteStartElement(collectionName); 
} 

public override void PostExecute() 
{   

    xWriter.WriteEndElement(); 
    xWriter.WriteEndDocument(); 
    xWriter.Close(); 
} 

public override void Input0_ProcessInputRow(Input0Buffer Row) 
{ 
    Type rowType = Row.GetType(); 
    PropertyInfo columnValue; 

    xWriter.WriteStartElement(rowName); 
    foreach (IDTSInputColumn100 column in this.ComponentMetaData.InputCollection[0].InputColumnCollection) 
    { 
     columnValue = rowType.GetProperty(column.Name); 
     xWriter.WriteStartElement(column.Name); 

     Object value = columnValue.GetValue(Row, null); 
     if(value != null) 
     { 
      xWriter.WriteValue(value.ToString()); 

     } 

     xWriter.WriteEndElement(); 
    }  
} 

我如何添加架構信息到這一點?我看過「WriteXmlSchema」的教程,但這似乎只適用於一個數據集(我沒有使用)。

我真的很感謝您能提供任何幫助。

+0

模式用於驗證XML。所以,你必須有一個現有的模式,或者你需要創建一個模式。您的客戶可能假設您正在根據模式驗證XML,並且他們希望通過檢查模式來查看驗證標準。因此,您可能希望根據項目預期重新與客戶同步。 – David

回答

1

如果您只需引用xml文檔中的現有模式,它只是根元素中的一個屬性。見this tutorial re XML schemas。在該網頁上的例子,寫的參考架構,之後 xWriter.WriteStartElement(collectionName); 添加

xWriter.WriteAttributeString("xsi:schemalocation", "http://www.w3schools.com note.xsd") 

如果你需要創建XSD架構文檔,有各種方式來做到這一點。我建議使用Google搜索「從...創建xsd」。

+0

工作正常!謝謝 – goalie35