【问题标题】:How to create this type of xml using xelement in c#如何在 C# 中使用 xelement 创建这种类型的 xml
【发布时间】:2019-11-01 07:46:27
【问题描述】:

这是我需要的:

<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,不能包含在名称中。”

【问题讨论】:

  • 到目前为止,您尝试了什么?你被困在哪里了?有数以千计的关于 xml 序列化的问题和主题,无论是使用 Linq2Xml,还是使用 XmlSerializer,甚至使用 XmlReader
  • 我正在使用 xelement 在 c# 中创建这种类型的 xml,但出现错误
  • 请准确显示您尝试过的内容以及遇到的错误。我们不喜欢猜测您可能的问题。
  • 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,不能包含在名称中。”

标签: c# .net xml


【解决方案1】:

您至少需要在父元素中声明命名空间。 看看这是否能让你开始:

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>

https://dotnetfiddle.net/zedBb3.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多