如何将XML文件转换为C#对象?

huangapple go评论59阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Main&gt;
  &lt;Group&gt;
	  &lt;NV&gt;BasicPars&lt;/NV&gt;
	  &lt;Property&gt;
		  &lt;NV&gt;Name&lt;/NV&gt;
		  &lt;Value&gt;wzorzec&lt;/Value&gt;
	  &lt;/Property&gt;
	  &lt;Property&gt;
		  &lt;NV&gt;UseFixedSizes&lt;/NV&gt;
		  &lt;Value&gt;False&lt;/Value&gt;
	  &lt;/Property&gt;
  &lt;/Group&gt;
  &lt;Group&gt;EventPars&lt;/Group&gt;
  &lt;Group&gt;Registers&lt;/Group&gt;
&lt;/Main&gt;

My classes:

[XmlRoot(ElementName=&quot;Property&quot;)]
public class Property { 

	[XmlElement(ElementName=&quot;NV&quot;)] 
	public string NV { get; set; } 

	[XmlElement(ElementName=&quot;Value&quot;)] 
	public string Value { get; set; } 
}

[XmlRoot(ElementName=&quot;Group&quot;)]
public class Group { 

	[XmlElement(ElementName=&quot;NV&quot;)] 
	public string NV { get; set; } 

	[XmlElement(ElementName=&quot;Property&quot;)] 
	public List&lt;Property&gt; Property { get; set; } 
}

[XmlRoot(ElementName=&quot;Main&quot;)]
public class Main { 

	[XmlElement(ElementName=&quot;Group&quot;)] 
	public List&lt;Group&gt; 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=&quot;NV&quot;)] 
    public string NV { get; set; } 

    [XmlElement(ElementName=&quot;Value&quot;)] 
    public string Value { get; set; } 
}

public class Group { 

    [XmlElement(ElementName=&quot;NV&quot;)] 
    public string NV { get; set; } 

    [XmlElement(ElementName=&quot;Property&quot;)] 
    public List&lt;Property&gt; Property { get; set; }
	
	[XmlText]
    public string InnerText { get; set; }
}

[XmlRoot(ElementName=&quot;Main&quot;)]
public class Main { 

    [XmlElement(ElementName=&quot;Group&quot;)] 
    public List&lt;Group&gt; Group { get; set; } 
}

Which deserializes correctly.

答案2

得分: 0

public class Serializer
{
public T Deserialize(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();
    }
}

}

英文:
public class Serializer
    {       
        public T Deserialize&lt;T&gt;(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&lt;T&gt;(T ObjectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());

            using (StringWriter textWriter = new StringWriter())
            {
                xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                return textWriter.ToString();
            }
        }
    }

huangapple
  • 本文由 发表于 2023年7月27日 15:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定