如何在同一个元素中添加许多属性元素?

时间:2015-09-29 12:09:37

标签: c# xml

我想在同一个元素中添加许多具有某些属性的元素。这是我的代码,

<ABC>
  <DEF>
    <GEH Name="" Button="" />
  </DEF>
  <DEF>
    <GEH Name="" Button="" />
  </DEF>
  <DEF>
    <GEH Name="" Button="" />
  </DEF>
</ABC>

它提供如下输出:

   <ABC>
      <DEF>
        <GEH Name="" Button="" />
        <GEH Name="" Button=""/>
        <GEH Name="" Button=""/>
      </DEF>
    </ABC>

但我想输出

import maya.cmds as cmds

######### Delete all existing Shader ############## 
cmds.delete (cmds.ls(type='shadingDependNode'))
cmds.delete (cmds.ls(type='shadingEngine'))
######### Delete all existing Shader ##############

######### Delete all Keyframes between 0 and 20 ##############
cmds.setKeyframe( 'lambert1', t='1', at='transparency'  )
cmds.cutKey ( t=(0,20) )
######### Delete all Keyframes ##############

#create Blau shader
Blau = cmds.shadingNode('phong', asShader=True, n='Blau', )
cmds.setAttr(Blau + '.color', 0.163,0.284,0.5)


#create KeyFrame Transparency ###Blau####
cmds.setKeyframe( 'Blau', t='1', at='transparency'  )
cmds.setAttr ( 'Blau.transparency', 1,1,1, type = "double3")
cmds.setKeyframe( 'Blau', t='15', at='transparency' )

######### update Time to 2 and back to 0  ##############
cmds.currentTime ( 2, update=True )
cmds.currentTime ( 0, update=True )
######### update Time to 2 and back to 0  ##############

请不要忽视循环。我只想输出for循环。请指导我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

基本上,for循环 包含GEH元素的创建。您可以在循环外移动DEF元素的创建...

但是,我也强烈建议使用LINQ to XML而不是旧的XmlDocument API ......它更简单。在这种情况下,代码如下所示:

var doc = new XDocument(
    new XElement("ABC",
        new XElement("DEF",
            Enumerable.Range(1, 3)
                      .Select(ignored => new XElement("GEH",
                           new XAttribute("Name", ""),
                           new XAttribute("Button", "")
                      )
        )
    )
);

答案 1 :(得分:0)

DEF元素的声明向上移动一级。

XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("ABC");
doc.AppendChild(root);
XmlElement id = doc.CreateElement("DEF");

for (int i = 0; i < 3; i++)
{
    XmlElement anotherid;
    anotherid = doc.CreateElement("GEH");
    anotherid.SetAttribute("Name", "");
    anotherid.SetAttribute("Button", "");
    root.AppendChild(id);
    id.AppendChild(anotherid);

}
doc.Save("dummyxml.xml");

生成

<ABC>
  <DEF>
    <GEH Name="" Button="" />
    <GEH Name="" Button="" />
    <GEH Name="" Button="" />
  </DEF>
</ABC>

答案 2 :(得分:0)

刚刚将代码移到for循环之外

XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("ABC");
            doc.AppendChild(root);

            XmlElement anotherid;
            XmlElement id;
            id = doc.CreateElement("DEF");

            for (int i = 0; i < 3; i++)
            {
                anotherid = doc.CreateElement("GEH");
                anotherid.SetAttribute("Name", "");
                anotherid.SetAttribute("Button", "");
                root.AppendChild(id);
                id.AppendChild(anotherid);

            }
            doc.Save(@"C:\dummyxml.xml");​