c#中的XML -为新的XML元素添加命名空间

本文关键字:XML 元素 添加 命名空间 中的 | 更新日期: 2023-09-27 17:53:05

我发布的XML可能很大,但我的问题相当简单。请看看第二次元标签。第一个维度是自动生成的。第二个是我在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>

c#中的XML -为新的XML元素添加命名空间

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

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