C#中的XML - 将命名空间添加到新的XML元素

时间:2015-06-03 10:40:35

标签: c# xml ssis xml-namespaces

我发布的XML可能很大,但我的问题很简单。请查看第二个Dimensions标记。第一个维度是自动生成的。第二个是我用C#脚本创建的。 问题是:如何使我的Dimension标签与生成的名称空间完全相同(“xsi:type =”RegularMeasureGroupDimension“)? 谢谢你的帮助!

这是我用来生成Dimension的C#代码:

XmlNode dimensions2 = doc.DocumentElement.ChildNodes[1].ChildNodes[0].ChildNodes[6].ChildNodes[0].ChildNodes[5];
            dimensions2.AppendChild(dimension2);
        //dimension2.SetAttribute("xmlns","http://schemas.microsoft.com/analysisservices/2003/engine");
           // dimension2.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XmlSchema-instance");
            dimension2.SetAttribute("type", "http://www.w3.org/2001/XmlSchema-instance", "RegularMeasureGroupDimension");

这是XML文件:

<Create xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
  <ParentObject>
    <DatabaseID>test_szymon</DatabaseID>
  </ParentObject>
  <ObjectDefinition>
    <Cube xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400">
      <ID>Test</ID>
      <Name>Test</Name>
      <Annotations>(...)</Annotations>
      <Language>1033</Language>
      <Collation>English</Collation>
      <Dimensions>(...)</Dimensions>
      <MeasureGroups>
        <MeasureGroup>
          <ID>FAKT</ID>
          <Name>FAKT</Name>
          <Measures>
            <Measure>
              <ID>Miara1</ID>
              <Name>Miara1</Name>
              <DataType>Integer</DataType>
              <Source>
                <DataType>Integer</DataType>
                <Source xsi:type="ColumnBinding">
                  <TableID>dbo_FACT</TableID>
                  <ColumnID>measure1</ColumnID>
                </Source>
              </Source>
            </Measure>
            <Measure>
              <ID>FACT</ID>
              <Name>FACT</Name>
              <AggregateFunction>Count</AggregateFunction>
              <DataType>Integer</DataType>
              <Source>
                <DataType>Integer</DataType>
                <DataSize>4</DataSize>
                <Source xsi:type="RowBinding">
                  <TableID>dbo_FAKT</TableID>
                </Source>
              </Source>
            </Measure>
          </Measures>
          <StorageMode>Molap</StorageMode>
          <ProcessingMode>Regular</ProcessingMode>
          <Dimensions>
            <Dimension xsi:type="RegularMeasureGroupDimension">
              <CubeDimensionID>WYMIAR</CubeDimensionID>
              <Attributes>
                <Attribute>
                  <AttributeID>Id Wymiar</AttributeID>
                  <KeyColumns>
                    <KeyColumn>
                      <DataType>Integer</DataType>
                      <NullProcessing>UnknownMember</NullProcessing>
                      <Source xsi:type="ColumnBinding">
                        <TableID>dbo_FAKT</TableID>
                        <ColumnID>id_wymiar</ColumnID>
                      </Source>
                    </KeyColumn>
                  </KeyColumns>
                  <Type>Granularity</Type>
                </Attribute>
              </Attributes>
            </Dimension>
<Dimension d7p1:type="RegularMeasureGroupDimension" xmlns:d7p1="http://www.w3.org/2001/XmlSchema-instance">
              <CubeDimensionID>WYMIAR2</CubeDimensionID>
              <Attributes>
                <Attribute>
                  <AttributeID>Id Wymiar2</AttributeID>
                  <KeyColumns>
                    <KeyColumn>
                      <DataType>Integer</DataType>
                      <NullProcessing>UnknownMember</NullProcessing>
                      <Source xmlns:xsi="http://www.w3.org/2001/XmlSchema-instance" xsi:type="ColumnBinding">
                        <TableID>dbo_FAKT</TableID>
                        <ColumnID>id_wymiar</ColumnID>
                      </Source>
                    </KeyColumn>
                  </KeyColumns>
                  <Type>Granularity</Type>
                </Attribute>
              </Attributes>
            </Dimension>
          </Dimensions>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用XmlDocument.CreateAttribute(prefix, localName, namespaceURI)方法,该方法允许我们指定属性的前缀,例如:

var attr = doc.CreateAttribute("xsi", "type", "http://www.w3.org/2001/XmlSchema-instance");
attr.Value = "RegularMeasureGroupDimension";
dimension2.SetAttributeNode(attr);