[英]Creating XML with LINQ in C#
I am trying to create the following XML document: 我正在尝试创建以下XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>insert</operation>
<object>Contact</object>
<contentType>CSV</contentType>
</jobInfo>
I have the following code (just starting out): 我有以下代码(刚开始):
XElement _obj = new XElement("?xml",
new XAttribute("version", "1.0"),
new XAttribute("encoding", "UTF-8")
);
The error I get is: 我得到的错误是:
Name cannot begin with the '?' 名称不能以“?”开头 character, hexadecimal value 0x3F. 字符,十六进制值0x3F。
I am new to creating XML with LINQ in C# and was just wondering if I am going about it the right way... How can I create the XML document I am trying to create? 我是不熟悉C#中使用LINQ创建XML的新手,只是想知道我是否正以正确的方式...如何创建要创建的XML文档?
You seem to be looking for an XDeclaration
. 您似乎正在寻找XDeclaration
。 These are automatically inserted, so you don't need to worry about them. 这些是自动插入的,因此您不必担心它们。 If you really need it though: 如果您确实需要它,请执行以下操作:
XDeclaration _obj = new XDeclaration("1.0", "utf-8", "");
XNamespace dl = "http://www.force.com/2009/06/asyncapi/dataload";
XElement jobInfo = new XElement(dl + "jobInfo",
new XElement(dl + "operation", "insert"),
new XElement(dl + "object", "Contact"),
new XElement(dl + "contentType", "CSV")
);
jobInfo.Save("info.xml");
should do and write an XML declaration <?xml version="1.0" encoding="UTF-8"?>
without any need to create it explicitly. 应该这样做并编写XML声明<?xml version="1.0" encoding="UTF-8"?>
而无需显式创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.