英文:
How to convert xml file to C# object?
问题
我正在尝试将这个 XML 文件转换为 C# 对象,但是不起作用。我无法获取 Group 标签之间的文本。类的结构允许我获取 Group 标签内部的子元素,但是当 Group 标签内只有一个文本时,我无法选择它。
以下是 XML 结构和我的类:
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<Main>
<Group>
<NV>BasicPars</NV>
<Property>
<NV>Name</NV>
<Value>wzorzec</Value>
</Property>
<Property>
<NV>UseFixedSizes</NV>
<Value>False</Value>
</Property>
</Group>
<Group>EventPars</Group>
<Group>Registers</Group>
</Main>
我的类:
[XmlRoot(ElementName="Property")]
public class Property {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="Group")]
public class Group {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Property")]
public List<Property> Property { get; set; }
}
[XmlRoot(ElementName="Main")]
public class Main {
[XmlElement(ElementName="Group")]
public List<Group> Group { get; set; }
}
英文:
I'm trying to convert this xml file to C# object but it doesn't work. I can't to get text between Group tags. The classes structure let me to get childs inside Group tag, but when Group tag has just one text inside, I can't select it.
I give the XML structure and my classes.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<Main>
<Group>
<NV>BasicPars</NV>
<Property>
<NV>Name</NV>
<Value>wzorzec</Value>
</Property>
<Property>
<NV>UseFixedSizes</NV>
<Value>False</Value>
</Property>
</Group>
<Group>EventPars</Group>
<Group>Registers</Group>
</Main>
My classes:
[XmlRoot(ElementName="Property")]
public class Property {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="Group")]
public class Group {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Property")]
public List<Property> Property { get; set; }
}
[XmlRoot(ElementName="Main")]
public class Main {
[XmlElement(ElementName="Group")]
public List<Group> Group { get; set; }
}
答案1
得分: 3
首先,只需要在根对象(即Main
)上使用XmlRoot
。从其他对象中删除它。
为了捕获元素内部的文本,可以使用[XmlText]
(参考链接:https://learn.microsoft.com/en-us/dotnet/standard/serialization/attributes-that-control-xml-serialization)。
修正后的类如下:
public class Property {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Value")]
public string Value { get; set; }
}
public class Group {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Property")]
public List<Property> Property { get; set; }
[XmlText]
public string InnerText { get; set; }
}
[XmlRoot(ElementName="Main")]
public class Main {
[XmlElement(ElementName="Group")]
public List<Group> Group { get; set; }
}
这样可以正确进行反序列化(参考链接:https://dotnetfiddle.net/RrBTfC)。
英文:
First, you only need XmlRoot
on the root object (that is, Main
). Remove it from the others.
In order to capture the text inside an element, use [XmlText]
.
The fixed classes are:
public class Property {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Value")]
public string Value { get; set; }
}
public class Group {
[XmlElement(ElementName="NV")]
public string NV { get; set; }
[XmlElement(ElementName="Property")]
public List<Property> Property { get; set; }
[XmlText]
public string InnerText { get; set; }
}
[XmlRoot(ElementName="Main")]
public class Main {
[XmlElement(ElementName="Group")]
public List<Group> Group { get; set; }
}
Which deserializes correctly.
答案2
得分: 0
public class Serializer
{
public T Deserialize
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}
英文:
public class Serializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论