如何在c++中创建XML::Linq XElement,而不是c#

how to create a XML::Linq XElement in C++, not C#

本文关键字:XElement Linq c++ 创建 XML      更新时间:2023-10-16

好的,我已经使用c#有一段时间了,在c#中创建一个新的xElement很简单

XElement NewElement = new XElement(NodeName, new XAttribute("Setting", Nodevalue));

其中NodeName和Nodevalue为字符串。简单。

但是我们试图在一些速度测试中挤出每一微秒,所以我们将项目转换为c++(不是我的强项)

我得到的是这个

XElement^ XMLHelper::QElement(String^ NodeName, String^ Nodevalue) { char NodeSetting[8] = "Setting";
XAttribute^ NewAttribute = gcnew XAttribute(gcnew XName(),Nodevalue)); XElement^ Built = gcnew XElement(NodeName, NewAttribute); return Built; }
但是我不知道如何将XAttribute名称设置为"Setting"

微软的文档https://msdn.microsoft.com/en-us/library/system.xml.linq.xattribute (v = vs.100) . aspx引用构造函数,但它们的c++示例都返回"目前没有可用的代码示例,或者可能不支持此语言。"

首先,您使用的不是标准的c++,而是托管的c++ (c++/CLI)。不同之处在于:标准c++只处理本机内存;c++/CLI是基于CLR(公共语言运行时)构建的,它可以访问本机和托管内存。

对于你的代码:

char NodeSetting[8] = "Setting";

这一行定义了一个本地变量,而不是托管变量。而对于XAttribute的名称,它需要一个管理对象(一个引用对象),所以您应该使用:

 String^ NodeSetting = gcnew String("Setting");

终于明白了。您不能使用gcnew XName(),因为您不能分配名称你不能只使用XAttribute("STRING",Value);

但是你可以使用XAttribute(gcnew String(" String "),Value);