c# - 如何在 C# 中使用 xelement 创建这种类型的 xml
问题描述
这就是我需要的:
<EuropeanSale>
<VATCore:SubmittersReference>001</VATCore:SubmittersReference>
<VATCore:CountryCode>AT</VATCore:CountryCode>
<VATCore:CustomerVATRegistrationNumber>U52375709</VATCore:CustomerVATRegistrationNumber>
<VATCore:TotalValueOfSupplies>1000</VATCore:TotalValueOfSupplies>
<VATCore:TransactionIndicator>2</VATCore:TransactionIndicator>
</EuropeanSale>
这是我的代码:
XElement temp= new XElement("EuropeanSale",
new XElement("Vat:SubmittersReference", item.SubmittersReference),
new XElement("Vat:CountryCode", item.CountryCode),
new XElement("CustomerVATRegistrationNumber", item.CustomerVATRegistrationNumber),
new XElement("Vat:TotalValueOfSupplies", item.TotalValueOfSupplies),
new XElement("Vat:TransactionIndicator", item.TransactionIndicator) );
这是一个例外:
“':' 字符,十六进制值 0x3A,不能包含在名称中。”
解决方案
您至少需要在父元素中声明命名空间。看看这是否能让你开始:
using System;
using System.Xml.Linq;
public class Program
{
public static void Main()
{
// Create an XML tree in a namespace, with a specified prefix
XNamespace ns = "http://example.com";
XElement root = new XElement("Root",
new XAttribute(XNamespace.Xmlns + "VATCore", "http://example.com"),
new XElement(ns + "Child", "child content")
);
Console.WriteLine(root);
}
}
以上输出
<Root xmlns:VATCore="http://example.com">
<VATCore:Child>child content</VATCore:Child>
</Root>
推荐阅读
- reactjs - 我的羽毛笔 reactjs 应用程序的另存为 PDF 按钮
- react-native - React-native-marketing cloud 是否包含用于在其 SDK 中接收消息的“onNotification”功能?
- firebase - Streambuilder 没有在 Flutter 中更新
- python - Tkinter 标签实时更新
- python - 如何在 Spark 中关闭舍入?
- python - Python 主导入文件
- javascript - 当添加在for循环中时如何添加到变量
- .net - WinUI-3 应用程序能否在 Windows Server 2016 上流畅运行?
- python - 将项目从一个树视图移动到另一个树视图的同一行
- python - 401 HTTP 响应,当我从 praw.ini 加载客户端密码和客户端 ID 时