0

名前空間を使用して XML を作成していますが、URL は名前空間の "プレフィックス" ではなく XAttribute 要素に記述されています。これが出力として得られるものです。

<xmi:XMI xmlns:uml="http://www.omg.org/spec/UML/20110701"
         xmlns:xmi="http://www.omg.org/spec/XMI/20110701">   
   <xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5" />  
       <uml:Model xmi:type="{http://www.omg.org/spec/UML/20110701}Model" name="EA_Model">
          <packagedElement xmi:type="{http://www.omg.org/spec/UML/20110701}Package" xmi:id="1212" name="Logical View">
          <packagedElement xmi:type="{http://www.omg.org/spec/UML/20110701}Class" xmi:id="35798" name="MyClass">

しかし、私はこのようなものを生成する必要があります..

<xmi:XMI xmlns:uml="http://www.omg.org/spec/UML/20110701"
         xmlns:xmi="http://www.omg.org/spec/XMI/20110701">   
   <xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5" />  
       <uml:Model xmi:type="uml:Model" name="EA_Model">
          <packagedElement xmi:type="uml:Package" xmi:id="1212" name="Logical View">
          <packagedElement xmi:type="uml:Class" xmi:id="35798" name="MyClass">

私のコード:

XNamespace uml = "http://www.omg.org/spec/UML/20110701";
XNamespace xmi = "http://www.omg.org/spec/XMI/20110701";

XElement rootElement = new XElement(xmi + "XMI", new XAttribute(XNamespace.Xmlns + "uml", "http://www.omg.org/spec/UML/20110701"), new XAttribute(XNamespace.Xmlns + "xmi", "http://www.omg.org/spec/XMI/20110701"));
doc.Add(rootElement);

rootElement.Add(new XElement(xmi+"Documentation", new XAttribute("exporter", "Enterprise Architect"), new XAttribute("exporterVersion", "6.5")));

XElement modelNode = new XElement(uml + "Model", new XAttribute(xmi + "type", uml + "Model"), new XAttribute("name", "EA_Model"));
rootElement.Add(modelNode);

XElement logicalViewElement = new XElement("packagedElement", new XAttribute(xmi + "type", uml + "Package"), new XAttribute(xmi + "id", project.Id), new XAttribute("name", "Logical View"));
modelNode.Add(logicalViewElement);

Dictionary<string, XElement> entityNodes = new Dictionary<string, XElement>();
foreach (BusinessEntityDataObject entity in project.BusinessEntities)
{
    XElement entityElement = 
        new XElement("packagedElement", 
           new XAttribute(xmi+"type",uml+"Class"),
           new XAttribute(xmi+"id",entity.Id), 
           new XAttribute("name",entity.Name));
4

1 に答える 1

0

XNamespace を文字列値と連結する代わりに

 new XAttribute(xmi + "type", uml + "Class")

属性値を手動で構築する必要があります。

 new XAttribute(xmi + "type", "uml:Class")

XAttributeコンストラクターには 2 つのパラメーターがあります。1 つ目は simple を想定XNameし、2 つ目は simple を想定していobjectます。と組み合わせるXNamespaceと、 に等しいのような結果stringが得られます。したがって、型は文字列からの暗黙的な変換を定義しており、この文字列はインスタンスに変換できます。stringxmi.ToString() + attributeName"{http://www.omg.org/spec/XMI/20110701}type"XNodeXName

2 番目のケースuml + "Class"でも string に結合されます"{http://www.omg.org/spec/UML/20110701}Class"。ただし、コンストラクターの 2 番目の引数は typeobjectである必要があり、この文字列はその要件を満たしているため、何も変換されずXName、結合された文字列が属性値として取得されます。

于 2013-10-21T09:41:34.420 に答える