c# - 带有额外标签的 XML 响应
问题描述
我正在开发 Web API 服务并返回 XML 作为响应。
下面是我的模型课
public class School
{
public List<StudentDetails> students {get;set;}
}
public class StudentDetails
{
public string Name {get;set;}
public int Age {get;set;}
}
我的控制器操作方法代码
School test = new School();
StudentDetails s1 = new StudentDetails();
s1.Name = "ABC"; s1.Age=25;
StudentDetails s2 = new StudentDetails();
s2.Name = "DEF"; s2.Age=35;
test.students.Add(s1);
test.students.Add(s2);
return Request.CreateResponse(HttpStatusCode.OK, test);
我的 XML 响应
<School xmln:i="http......> //not typing complete text here
<students>
<StudentDetails>
<Age>25</Age>
<Name>ABC</Name>
</StudentDetails>
<StudentDetails>
<Age>35</Age>
<Name>DEF</Name>
</StudentDetails>
</students>
</School>
作为回应,为什么我会得到<StudentDetails>
标签?相反,我期待<students>
到位。
解决方案
您可以尝试添加将更改序列化 XML 元素名称的类装饰。看看下面。
c#,版本 1
void Main()
{
const string outputFile = @"e:\temp\SerializedFile.xml";
StudentDetails s1 = new StudentDetails();
s1.Name = "ABC"; s1.Age = 25;
StudentDetails s2 = new StudentDetails();
s2.Name = "DEF"; s2.Age = 35;
School school = new School();
school.students = new List<StudentDetails>();
school.students.Add(s1);
school.students.Add(s2);
// save new XML file, serialized from classes
using (StreamWriter sw = new StreamWriter(outputFile))
{
XmlSerializer x = new XmlSerializer(typeof(School));
// to remove not needed namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
// create new XML file
x.Serialize(sw, school, ns);
}
Console.WriteLine("Classes were serialized as '{0}' file.", outputFile);
}
[XmlRoot("school")]
public class School
{
[XmlElement(ElementName = "student")]
public List<StudentDetails> students { get; set; }
}
public class StudentDetails
{
public string Name { get; set; }
public int Age { get; set; }
}
输出
<?xml version="1.0" encoding="utf-8"?>
<school>
<student>
<Name>ABC</Name>
<Age>25</Age>
</student>
<student>
<Name>DEF</Name>
<Age>35</Age>
</student>
</school>
c#,版本 2
void Main()
{
string outputXML = string.Empty;
StudentDetails s1 = new StudentDetails();
s1.Name = "ABC"; s1.Age = 25;
StudentDetails s2 = new StudentDetails();
s2.Name = "DEF"; s2.Age = 35;
School school = new School();
school.students = new List<StudentDetails>();
school.students.Add(s1);
school.students.Add(s2);
// save new XML file, serialized from classes
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "\t";
// to remove BOM
settings.Encoding = new UTF8Encoding(false);
using (var sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
XmlSerializer x = new XmlSerializer(typeof(School));
// to remove not needed namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
// create new XML file
x.Serialize(xw, school, ns);
}
outputXML = sw.ToString();
}
Console.WriteLine("Classes were serialized as an XML string:{1}{0}", outputXML, Environment.NewLine);
}
// Define other methods and classes here
[XmlRoot("school")]
public class School
{
[XmlElement(ElementName = "student")]
public List<StudentDetails> students { get; set; }
}
public class StudentDetails
{
public string Name { get; set; }
public int Age { get; set; }
}
推荐阅读
- python - 如何在 pandas 和 python 的 grouby 数据图中显示线条的标签
- html - 浏览器因嵌入网站而停止工作,但仅在显示图标时
- ffmpeg - Raspberry Pi Zero W 上的 FFmpeg 安装:对 __atomic 的未定义引用
- sql - 在 Postgres 中搜索部分存在于字符串中的多个单词
- linux - 如何检查我的交通控制规则的运行情况?
- r - 在 r 处转换并保持原始列
- amazon-web-services - 如何在 ecs 中将 aws xray 作为所有其他容器可以将数据发送到的守护进程运行
- javascript - HERE Maps 未在 iPhone 上加载地图图像
- amazon-web-services - 如何更新运行 AWS ECS FARGATE 任务定义的 Docker 镜像
- reactjs - 将 createMuiTheme 中定义的样式与 Material-UI 的 useStyles 一起使用的正确方法是什么?