Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将属性序列化为具有默认属性的元素_C#_Xml Serialization - Fatal编程技术网

C# 如何将属性序列化为具有默认属性的元素

C# 如何将属性序列化为具有默认属性的元素,c#,xml-serialization,C#,Xml Serialization,我需要用一个属性将这个类序列化为xml,该属性需要作为一个元素,属性名作为默认属性的值 类请求到 { 公共字符串ZipCode{get;set;} } 作为 不能直接从对象模型的形状通过XmlSerializer来实现这一点;基本上,XmlSerializer假定您的模型和xml或多或少是相同的。实现IXmlSerializable显然是不值得的。我建议使用XDocument将其序列化: string zip = "abc"; var el = new XElement("RequestDt

我需要用一个属性将这个类序列化为xml,该属性需要作为一个元素,属性名作为默认属性的值

类请求到
{
公共字符串ZipCode{get;set;}
} 
作为

不能直接从对象模型的形状通过
XmlSerializer
来实现这一点;基本上,
XmlSerializer
假定您的模型和xml或多或少是相同的。实现
IXmlSerializable
显然是不值得的。我建议使用
XDocument
将其序列化:

string zip = "abc";
var el = new XElement("RequestDto",
    new XElement("Parameter",
        new XAttribute("name", "zipcode"),
        zip
    )
);
它(通过
.ToString()
)给出:


abc
string zip = "abc";
var el = new XElement("RequestDto",
    new XElement("Parameter",
        new XAttribute("name", "zipcode"),
        zip
    )
);
<RequestDto>
  <Parameter name="zipcode">abc</Parameter>
</RequestDto>