gpt4 book ai didi

c# - 从 DataContract 中删除命名空间不起作用

转载 作者:行者123 更新时间:2023-11-30 13:06:53 27 4
gpt4 key购买 nike

我必须有两个简单的序列化/反序列化方法,

映射:

[System.Runtime.Serialization.DataContract(Namespace = "", Name = "PARAMS")]
public sealed class CourseListRequest {

[DataMember(Name = "STUDENTID")]
public int StudentId { get; set; }

[DataMember(Name = "YEAR")]
public string Year { get; set; }

[DataMember(Name = "REQUESTTYPE")]
public int RequestType { get; set; }
}

public static string Serialize<T>(this T value) {
if (value == null) throw new ArgumentNullException("value");
try {
var dcs = new DataContractSerializer(typeof (T));
string xml;

using (var ms = new MemoryStream()) {
dcs.WriteObject(ms, value);
xml = Encoding.UTF8.GetString(ms.ToArray());
}
return xml;
}
catch (Exception e) {
throw;
}
}

public static T Deserialize<T>(this string xml) where T : class {

if (string.IsNullOrEmpty(xml)) {
return default(T);
}
try {
var dcs = new DataContractSerializer(typeof (T));
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
ms.Position = 0;
return dcs.ReadObject(ms) as T;
}
}
catch (Exception e) {
throw;
}
}

result:

<PARAMS xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><REQUESTTYPE>36</REQUESTTYPE><STUDENTID>0</STUDENTID><YEAR>תשע</YEAR></PARAMS>

如何删除 xmlns:i="http://www.w3.org/2001/XMLSchema-instance"??关于序列化

最佳答案

切换到使用 XmlSerializer

System.Xml.Serialization.XmlSerializer

这将生成没有 namespace 的纯 XML

关于c# - 从 DataContract 中删除命名空间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17638155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com