简体   繁体   English

foreach-Loop的问题,以获取正确的XML文件输出

[英]Problems with the foreach-Loop, to get proper Output as an XML-File

i am currently working with Enterprise Architect(EA) and I am writing a little AddIn to extract the Attributes and the Attributevalues from the Model-Diagram and extract it to an XML-File. 我目前正在与Enterprise Architect(EA)合作,并且正在编写一些AddIn来从Model-Diagram中提取属性和Attributevalue,并将其提取到XML文件中。 How to use the EA-API is no Problem, but because I am pretty new to C#, I have problems with the C#-Implementation: 如何使用EA-API没问题,但是因为我对C#还是很陌生,所以我在C#实施方面遇到了问题:

First I post the Code, then a piece of the Output file and then an example what kind of output I want. 首先,我发布代码,然后发布一部分输出文件,然后举一个示例,说明我想要什么样的输出。 So at least the Question will have a structure, better than my code ;) 因此,至少Question具有比我的代码更好的结构;)

String writerString3;
String tagString3;
tagString3 = "";
writerString3 = "";


foreach (EA.Element thePackages in myPackage.Packages)
   { // myPackage = the package I select by MenuClick

    foreach (EA.Element theElements in myPackage.Elements)
     {

      foreach (EA.Attribute theAttribute in theElements.Attributes)
      {

        XElement toXML = new XElement(myPackage.Name.ToString(),
        (new XElement(theElements.Name.ToString(),
        (new XAttribute(theAttribute.Name.ToString(), theAttribute.Default.ToString())))));
        tagString3 = tagString3 + toXML;

       }
      writerString3 = writerString3 + tagString3;
      tagString3 = "";
      }
  }

  foreach (EA.Element theElements in myPackage.Elements)
  {

   foreach (EA.Attribute theAttribute in theElements.Attributes)
   {
    XElement toXML = new XElement(myPackage.Name.ToString(),
    (new XElement(theElements.Name.ToString(),
    (new XAttribute(theAttribute.Name.ToString(), theAttribute.Default.ToString())))));
    tagString3 = tagString3 + toXML;

    }
    writerString3 = writerString3 + tagString3;
    tagString3 = "";
      }

    TextWriter tw = new StreamWriter(myPackage.Name.ToString() + ".xml");
    tw.WriteLine(writerString3);
    tw.Close(); }

The Output looks like this: //shortened version 输出看起来像这样://缩短版本

<Application01>
  <App01 Name="App01" />
</Application01><Application01>
  <App01 ApplicationDomain="xyz" />
</Application01><Application01>
  <App01 AbstractionLevel="" />
</Application01><Application01>
  <App01_Communication Type="CAN" />
</Application01><Application01> 

It goes on like this, with all the Elements and Attributes listet in each line. 这样,每行中都有所有Elements and Attributes列表。 I know the code is wrong, but I cannot fix it in a proper way. 我知道代码是错误的,但是我无法以正确的方式修复它。 First I pick a package, iterate through the Elements and for each Element I want to output the related Atrributes plus values. 首先,我选择一个包,遍历元素,然后为每个元素输出相关的属性和值。 Eg: Element1 Attrbute11, Attribute12, Attribute13 Element2 Attrbute21, Attribute22, Attribute23 例如:Element1 Attrbute11,Attribute12,Attribute13 Element2 Attrbute21,Attribute22,Attribute23

And so on...My prefered XML-File should look like this: 依此类推...我偏爱的XML文件应如下所示:

<packageName>
  <Element1>
    <Attribute11>value11</Attribute11>
    <Attribute22>value22</Attribute22>
    <Attribute33>value33</Attribute33>
  </Element1>
  <Element2>
    <Attribute21>value21</Attribute21>
    <Attribute22>value22</Attribute22>
    <Attribute23>value23</Attribute23>
  </Element2>
</packageName>

I hope i could make my problem clear. 我希望我能解决我的问题。 I wouldn't ask if I couldn't do it on my own. 我不会问我是否不能自己做。 Of course if somebody know how to get the Attributes and Elements out of the Diagrams with a different way, I would be happy to hear that(besides the tools given by EA). 当然,如果有人知道如何以不同的方式从图中获取属性和元素,那么我很乐意听到(EA提供的工具之外)。 Thanks for reading and many thanks fpr helping ;) 感谢您的阅读,也非常感谢fpr的帮助;)

You've got two separate loops at the outermost level, first off. 首先,您在最外层有两个单独的循环。 This doesn't seem useful. 这似乎没有用。

Secondly, you are looping over the myPackage.Packages collection, then each time through that loop you do nothing with the loop variable myPackages but simply launch into the inner loop which does the work. 其次,您要遍历myPackage.Packages集合,然后每次通过该循环,您都不myPackages循环变量myPackages进行任何操作,而只是启动进入工作的内部循环。 So you end up with one copy of each element for each subpackage in myPackage (and none if there are no subpackages). 因此,最终得到myPackage中每个子包的每个元素的一个副本(如果没有子包,则没有一个)。 So clearly that has to go. 显然,这必须走。

Finally, setting up an XElement as a formatter and then successively copying the XML data into your own string is probably not the best solution. 最后,将XElement设置为格式化程序,然后将XML数据依次复制到您自己的字符串中可能不是最佳解决方案。 It's better to use XElement to construct the XML tree first and then dump the contents. 最好先使用XElement构造XML树,然后转储内容。

So it should look something like this (not tested, not compiled): 因此,它应该看起来像这样(未经测试,未经编译):

XElement xPkg = new XElement(myPackage.Name.ToString());

foreach (EA.Element theElement in myPackage.Elements)
{
    XElement xElem = new XElement(theElement.Name.ToString());
    xPkg.Add(xElem);
    foreach (EA.Attribute theAttribute in theElement.Attributes)
    {
        XAttribute xAttr = new XAttribute(theAttribute.Name.ToString(),
                                          theAttribute.Default.ToString()));
        xElem.Add(xAttr);
    }
}

TextWriter tw = new StreamWriter(myPackage.Name.ToString() + ".xml");
tw.WriteLine(xPkg.ToString());
tw.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM