gpt4 book ai didi

c# - 反序列化具有不同标签的 XML 相同项目

转载 作者:行者123 更新时间:2023-12-05 03:36:56 25 4
gpt4 key购买 nike

这是我想反序列化并在桌面应用程序中使用的 XML 示例,用户可以在其中插入这样的 XML:

   <Settings>
<FileName>C:\Users\myniceuser\Desktop\hello.xmldoc</FileName>
<Zones>
<Row1>
<Address>2</Address>strong text
<Zone>2</Zone>
<Installed>True</Installed>
</Row1>
<Row2>
<Address>3</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row2>
<Row3>
<Address>4</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row3>
<Row4>
<Address>5</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row4>
</Zones>
<Network_IP>010.000.008.072</Network_IP>
<Ethernet_Enable>true</Ethernet_Enable>
<Language>1</Language>
</Settings>

我实现的类是这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace MyApp.Models
{
[XmlRoot(ElementName = "Settings")]
public class XML_Settings
{

[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }


//[XmlElement(ElementName = "Zones")]
[XmlElement(ElementName ="Zones")]
public Zones_Points[] Zones_Points { get; set; }

[XmlElement(ElementName = "Network_IP")]
public string NetworkIP { get; set; }

[XmlElement(ElementName = "Ethernet_Enable")]
public bool EthernetEnable { get; set; }

[XmlElement(ElementName = "Language")]
public int Language { get; set; }
}
[XmlRoot(ElementName = "")]
public partial class Zones_Points
{
[XmlElement(ElementName ="Address")]
public int Address { get; set; }

[XmlElement(ElementName ="Zone")]
public int PointZone { get; set; }

[XmlElement(ElementName ="Installed")]
public bool Installed { get; set; }

}
}

我这样反序列化:

Models.XML_Settings inputted_xml = new();
using StreamReader stream_xml = new StreamReader(xmlfile_path, CodePagesEncodingProvider.Instance.GetEncoding(1253));

XmlSerializer serializer = new XmlSerializer(typeof(Models.XML_Settings));

try
{
Models.XML_Settings input = (Models.XML_GR6500)serializer.Deserialize(stream_xml);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

我希望我的应用程序像这样输入 XML 文件,并将它们的数据转换为我可以使用的类。

反序列化创建的对象“输入”得到了所有正确的数据,除了区域,那些在 XML 中具有“行”标签的区域。这些“行”每次都可能多于或少于 4。

如何将带有行标签的项目反序列化并输入到 Zones_Points 列表或数组中?

最佳答案

使用 IXmlSerializable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;

namespace ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(XML_Settings));
XML_Settings settings = (XML_Settings)serializer.Deserialize(reader);

}
}
[XmlRoot("Settings")]
public class XML_Settings
{


[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }

[XmlElement(ElementName = "Zones")]
public Zones_Points Zones_Point { get; set; }
[XmlElement(ElementName = "Network_IP")]
public string NetworkIP { get; set; }
[XmlElement(ElementName = "Ethernet_Enable")]
public bool EthernetEnable { get; set; }
[XmlElement(ElementName = "Language")]
public int Language { get; set; }
}
public partial class Zones_Points : IXmlSerializable
{
// Xml Serialization Infrastructure
[XmlElement(ElementName = "Zones")]
public List<Zones_Point> Points { get; set; }

public void WriteXml(XmlWriter writer)
{
}

public void ReadXml(XmlReader reader)
{

XElement zones = (XElement)XElement.ReadFrom(reader);
Points = new List<Zones_Point>();
foreach (XElement row in zones.Elements())
{
Zones_Point Point = new Zones_Point();
Points.Add(Point);
Point.Address = (int)row.Element("Address");
Point.PointZone = (int)row.Element("Zone");
Point.Installed = (Boolean)row.Element("Installed");
}

}

public XmlSchema GetSchema()
{
return (null);
}
}
public partial class Zones_Point
{
[XmlElement(ElementName = "Address")]
public int Address { get; set; }
[XmlElement(ElementName = "Zone")]
public int PointZone { get; set; }
[XmlElement(ElementName = "Installed")]
public bool Installed { get; set; }
}
}

关于c# - 反序列化具有不同标签的 XML 相同项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69523965/

25 4 0