gpt4 book ai didi

c# - XML 序列化没有得到预期的 xml 文件 c#

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:13 25 4
gpt4 key购买 nike

我反序列化了一个看起来像这样的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<NETWORK>
<ROUTES>
<ROUTE ID="RT_BALA_ORLS_R_111_119_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
<ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
<EXITSIGNAL>BALA_ORLN_G119</EXITSIGNAL>
<TRACKSECTIONIDS>
<TRACKSECTIONID>BALA_ORLS_OSTK.TrackingObject</TRACKSECTIONID>
<TRACKSECTIONID>BALA_ORLS_WBK_ORLN_EBK.TrackingObject</TRACKSECTIONID>
</TRACKSECTIONIDS>
<SUBROUTEIDS/>
<POINTENDIDS>
<POINTENDID POS="N">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
</POINTENDIDS>
</ROUTE>
<ROUTE ID="RT_BALA_ORLS_R_111_119D_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
<ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
<EXITSIGNAL>BALA_ORLN_G119D</EXITSIGNAL>
<TRACKSECTIONIDS>
<TRACKSECTIONID>BALA_ORLS_OSTK.TrackingObject</TRACKSECTIONID>
<TRACKSECTIONID>BALA_ORLS_ORLN_Z164.TrackingObject</TRACKSECTIONID>
</TRACKSECTIONIDS>
<SUBROUTEIDS/>
<POINTENDIDS>
<POINTENDID POS="R">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
</POINTENDIDS>
</ROUTE>
</ROUTES>
</NETWORK>

现在我正在尝试对反序列化数据进行一些处理,以便进行序列化并生成如下所示的 xml 文件:

<NXRoutes>
<NXRoute ID="1" OriginSignal="BALA_ORLS_G111" DestinationSignal="BALA_ORLN_G119">
<Path ID="1" Preferred="YES" SnowPlan="NO">RT_BALA_ORLS_R_111_119_1</Path>
</NXRoute>
<NXRoute ID="2" OriginSignal="BALA_ORLS_G111" DestinationSignal="BALA_ORLN_G119D">
<Path ID="1" Preferred="YES" SnowPlan="NO">RT_BALA_ORLS_R_111_119D_1</Path>
</NXRoute>
</NXRoutes>

这是我尝试过的,我得到一个只有 RootElement 的空 xml 文件:

static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(Network));
TextReader reader = new StreamReader(@"xml File Location");
object obj = deserializer.Deserialize(reader);
Network XmlData = (Network)obj;
reader.Close();
string OriginSignal = null;
Console.WriteLine("");
OriginSignal = Console.ReadLine();
var SelectedRoutes = new List<ROUTE>();

foreach (var route in XmlData.ROUTES)
{
if ((OriginSignal.Equals(route.ENTRANCESIGNAL)))
{
SelectedRoutes.Add(route);
}
}
//NXRoutes nxRoutes = new NXRoutes();
List<NXRoute> NXRouteList = new List<NXRoute>();
List<Path> paths = new List<Path>();
Path path = new Path();
NXRoute nxRoute = new NXRoute();
NXRoutes nxRoutes = new NXRoutes();
foreach (var item in SelectedRoutes)
{

nxRoute.ID = SelectedRoutes.Count.ToString();
nxRoute.OriginSignal = OriginSignal;
nxRoute.DestinationSignal = item.EXITSIGNAL;
path.ID = "1";
//nxRoute.Path.Add(path.ID);
path.Preferred = "YES";
path.SnowPlan = "NO";
path.PathInnerText = item.ID;
NXRouteList.Add(nxRoute);

///Console.WriteLine(item.ID);
}
//Console.ReadLine();
XmlSerializer serializer = new XmlSerializer(typeof(NXRoutes));
System.IO.TextWriter writer = new StreamWriter(@"xml File Location");
serializer.Serialize(writer, nxRoutes);
writer.Close();
}

当我开始调试时,对象nxRoutes为空

有我为序列化定义的类

[XmlRoot("NXRoutes")]
public class NXRoutes
{
[XmlElement("NXRoute")]
public List<NXRoute> NXRoute { get; set; }
}

public class NXRoute
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("OriginSignal")]
public string OriginSignal { get; set; }
[XmlAttribute("DestinationSignal")]
public string DestinationSignal { get; set; }
[XmlElement("Path")]
public List<Path> Path { get; set; }
}

public class Path
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("Preferred")]
public string Preferred { get; set; }
[XmlAttribute("SnowPlan")]
public string SnowPlan { get; set; }
[XmlText]
public string PathInnerText { get; set; }
}

反序列化:

[XmlRoot("NETWORK")]
public class Network
{
[XmlArrayItem("ROUTE")]
[XmlArray("ROUTES")]
public List<ROUTE> ROUTES { get; set; }
}

public class ROUTE
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("DIRECTION")]
public string DIRECTION { get; set; }
[XmlElement("ENTRANCESIGNAL")]
public string ENTRANCESIGNAL { get; set; }
[XmlElement("EXITSIGNAL")]
public string EXITSIGNAL { get; set; }
[XmlElement("POINTENDIDS")]
public POINTENDIDS POINTENDIDS { get; set; }
}

public class POINTENDIDS
{
[XmlElement("POINTENDID")]
public List<POINTENDID> POINTENDID { get; set; }
}

public class POINTENDID
{
[XmlAttribute("POS")]
public string POS { get; set; }
}

初学c#,能不能帮我弄出想要的xml文件

最佳答案

我强烈怀疑您遗漏了这行代码:

nxRoutes.NXRoute = NXRouteList;

您当前正在序列化 nxRoutes,但尚未以任何有意义的方式实际初始化 nxRoutes

关于c# - XML 序列化没有得到预期的 xml 文件 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54342937/

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