C# 当我创建XML时,XNamespace url是用XAttribute编写的
我正在创建一个带有名称空间的XML,但url是用XAttribute元素而不是名称空间“前缀”编写的 这就是我得到的输出C# 当我创建XML时,XNamespace url是用XAttribute编写的,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我正在创建一个带有名称空间的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" export
<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));
XUML=”http://www.omg.org/spec/UML/20110701";
XXmi=”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“”,新的XAttribute(XNamespace.Xmlns+“xmi”http://www.omg.org/spec/XMI/20110701"));
doc.Add(rootElement);
添加(新XElement(xmi+“文档”、新XAttribute(“导出器”、“企业架构师”)、新XAttribute(“导出器”、“6.5”));
XElement modelNode=新XElement(uml+“模型”、新XAttribute(xmi+“类型”、uml+“模型”)、新XAttribute(“名称”、“EA_模型”);
添加(modelNode);
XElement logicalViewElement=新XElement(“packagedElement”、新XAttribute(xmi+“type”、uml+“Package”)、新XAttribute(xmi+“id”、project.id)、新XAttribute(“名称”、“逻辑视图”);
添加(logicalViewElement);
Dictionary entityNodes=新字典();
foreach(项目中的BusinessEntityDataObject实体。BusinessEntities)
{
XElement entityElement=
新XElement(“packagedElement”,
新的XAttribute(xmi+“类型”,uml+“类”),
新的XAttribute(xmi+“id”,entity.id),
新的XAttribute(“name”,entity.name));
而不是将XNamespace与字符串值连接起来
new XAttribute(xmi + "type", uml + "Class")
您应该手动生成属性值:
new XAttribute(xmi + "type", "uml:Class")
XAttribute
构造函数有两个参数-第一个参数需要XName
,第二个参数需要简单的object
。当您将XNamespace
与string
组合时,您会得到string
结果,它看起来像xmi.ToString()+attributeName
,等于”{http://www.omg.org/spec/XMI/20110701}键入“
。因此,XNode
type定义了从字符串的隐式转换,该字符串可以转换为XName
实例
在第二种情况下,uml+“Class”
也组合成字符串“{http://www.omg.org/spec/UML/20110701}“类”
。但是构造函数的第二个参数应该是object
类型,并且该字符串满足该要求,因此不会将任何内容转换为XName
,并且您将组合字符串作为属性值