0

我需要从数据库中提取数据并通过 ssis 创建一个 xml 文件。到目前为止,我已经创建了一个脚本,可以成功创建一个 xml 文件(使用 XmlTextWriter),列出所有必要的数据,但是,他们也希望在文件中列出架构,我不知道该怎么做。

这是我当前创建 xml 文件的代码(在我的数据流中使用脚本组件):

StreamWriter sw;
XmlTextWriter xWriter;
String rowName;
String collectionName;
private int[] columnNames;    
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”的教程,但这似乎只适用于数据集(我没有使用)。

我非常感谢您能提供的任何帮助。

4

1 回答 1

1

如果您只需要引用 xml 文档中的现有模式,它只是根元素中的一个属性。请参阅本教程关于 XML 模式xWriter.WriteStartElement(collectionName); 在该页面上的示例中,在添加后写入对架构的引用

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

如果您需要创建 xsd 模式文档,有多种方法可以做到这一点。我建议谷歌搜索“从...创建 xsd”。

于 2012-10-18T20:46:01.400 回答