XML 到 c# 无法转换
本文关键字:转换 XML | 更新日期: 2023-09-27 18:18:27
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <DPDocument> <TradingPartnerDirectory> <Sender> <TradingPartner ParticipantType="Distributor"> <CompanyID>200408144</CompanyID> <CompanyName>Union Gas Ltd</CompanyName> </TradingPartner> </Sender> <Recipient> <TradingPartner ParticipantType="Retailer"> <CompanyID>DUNS24653</CompanyID> <CompanyName>Local Authority Services Limited</CompanyName> </TradingPartner> </Recipient> </TradingPartnerDirectory> <ContractInformation> <ContractID>SA1854</ContractID> <ContractName>GASAMO #1 EDA-ABC</ContractName> <Amendment>24</Amendment> <FirstDeliveryDate>11/1/2011</FirstDeliveryDate> <EffectiveDate>11/1/2011</EffectiveDate> <DeliveryArea>EDA</DeliveryArea> <TotalDCQ>546</TotalDCQ> <EnergyFactor>37.51</EnergyFactor> <EndUseLocationInformation> <AccountNumber>10109251120297</AccountNumber> <AccountName>TOWN OF DESERONTO</AccountName> <RateClass>01</RateClass> <ServiceClass>C</ServiceClass> <ForecastInformation> <BillingMonth>JAN</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>1116.0040</Consumption> <WeatherFactor>1.045723</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>FEB</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>1269.7390</Consumption> <WeatherFactor>0.937073</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>MAR</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>2064.0370</Consumption> <WeatherFactor>1.015836</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>APR</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>82.5610</Consumption> <WeatherFactor>0.982911</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>MAY</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>546.6140</Consumption> <WeatherFactor>0.972752</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>JUN</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>128.1130</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>JUL</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>5.6940</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>AUG</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>0</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>SEP</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>0</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>OCT</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>2.8470</Consumption> <WeatherFactor>0.993534</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>NOV</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>290.3890</Consumption> <WeatherFactor>1.0585</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>DEC</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>686.1140</Consumption> <WeatherFactor>0.938379</WeatherFactor> </ForecastInformation> </EndUseLocationInformation> <EndUseLocationInformation> <AccountNumber>10110651120442</AccountNumber> <AccountName>COMMUNITY CENTRE</AccountName> <RateClass>01</RateClass> <ServiceClass>C</ServiceClass> <ForecastInformation> <BillingMonth>JAN</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>4327.3610</Consumption> <WeatherFactor>1.045723</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>FEB</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>4811.3420</Consumption> <WeatherFactor>0.937073</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>MAR</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>5924.4990</Consumption> <WeatherFactor>1.015836</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>APR</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>4971.9730</Consumption> <WeatherFactor>0.982911</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>MAY</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>2302.5290</Consumption> <WeatherFactor>0.972752</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>JUN</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>761.1410</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>JUL</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>350.3160</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>AUG</BillingMonth> <BillingYear>2011</BillingYear> <Consumption>267.5140</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>SEP</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>42.7050</Consumption> <WeatherFactor>1</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>OCT</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>416.8490</Consumption> <WeatherFactor>0.993534</WeatherFactor> <WeatherFactor>0.993534</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>NOV</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>1389.3110</Consumption> <WeatherFactor>1.0585</WeatherFactor> </ForecastInformation> <ForecastInformation> <BillingMonth>DEC</BillingMonth> <BillingYear>2010</BillingYear> <Consumption>1641.4940</Consumption> <WeatherFactor>0.938379</WeatherFactor> </ForecastInformation> </EndUseLocationInformation> </DPDocument>
以上是我的XML代码..我无法将其转换为C#类 下面是我为该XML创建的C#类
public class DPDocument
{
public TradingPartnerDirectory tradingpartnerdirectory
{
get;
set;
}
public ContractInformation contractInformation
{
get;
set;
}
public DPDocument()
{
tradingpartnerdirectory = new TradingPartnerDirectory();
contractInformation = new ContractInformation();
}
}
public class TradingPartnerDirectory
{
public Sender sender
{
get;
set;
}
public Recipient recipient
{
get;
set;
}
public TradingPartnerDirectory()
{
sender = new Sender();
recipient = new Recipient();
}
}
public class Sender
{
public TradingPartner tradingPartner
{
get;
set;
}
public Sender()
{
tradingPartner = new TradingPartner();
}
}
public class Recipient
{
public TradingPartner tradingPartner
{
get;
set;
}
public Recipient()
{
tradingPartner = new TradingPartner();
}
}
public class TradingPartner
{
[XmlAttribute("ParticipantType")]
public string ParticipantType
{
get;
set;
}
public long CompanyID
{
get;
set;
}
public string CompanyName
{
get;
set;
}
}
public class ContractInformation
{
public string ContractID
{
get;
set;
}
public string ContractName
{
get;
set;
}
public int Amendment
{
get;
set;
}
public DateTime FirstDeliveryDate
{
get;
set;
}
public DateTime EffectiveDate
{
get;
set;
}
public string DeliveryArea
{
get;
set;
}
public int TotalDCQ
{
get;
set;
}
public decimal EnergyFactor
{
get;
set;
}
public List<EndUseLocationInformation> child
{
get;
set;
}
public ContractInformation()
{
child = new List<EndUseLocationInformation>();
}
}
public class EndUseLocationInformation
{
public long AccountNumber
{
get;
set;
}
public string AccountName
{
get;
set;
}
public int RateClass
{
get;
set;
}
public string ServiceClass
{
get;
set;
}
public List<ForecastInformation> childchild
{
get;
set;
}
public EndUseLocationInformation()
{
childchild = new List<ForecastInformation>();
}
}
public class ForecastInformation
{
public string BillingMonth
{
get;
set;
}
public int BillingYear
{
get;
set;
}
public decimal Consumption
{
get;
set;
}
public decimal WeatherFactor
{
get;
set;
}
public ForecastInformation()
{
}
}
我无法一次性将其转换为 C# DPDocument 对象,我尝试了所有方式,但我没有实现那个。最后,我采用了一个最糟糕的解决方案,例如采用单个节点列表并最终处理该列表,该方法对我有用。但我需要一个优雅的解决方案来解决我的问题。提前谢谢你:)
当您有一个有效的 XML 文档时,您可以执行两个步骤的过程:
-
运行随 .NET 框架一起安装的
xsd.exe
命令行工具,将 XML 转换为 XSD(XML 架构(:c:'> xsd.exe yourfile.xml
这将在第二步所需的同一位置创建一个名为
yourfile.xsd
的文件。 -
再次运行
xsd.exe
命令行工具,将 XML 架构转换为 C# 代码:c:'> xsd.exe /c yourfile.xsd
这将在同一位置创建一个名为
yourfile.cs
的文件,该文件包含反序列化正在处理的此特定 XML 所需的 C# 类和帮助程序结构。
现在,使用该 C# 文件,您应该能够执行以下操作:
XmlSerializer serializer = new XmlSerializer(typeof(DPDocument));
TextReader tr = new StreamReader(@"path-to-your-XML-file-here");
var result = serializer.Deserialize(tr);
并且您应该从 XML 文件中反序列化 DPDocument
类。
这种具体情况下,在尝试执行此操作时,您将偶然发现一个错误 - 这是因为XSD
工具根据您的 XML 输入文件做了一个很好的 - 但不完美的 - 工作。您需要手动修复此问题:
public partial class DPDocumentTradingPartnerDirectory {
private TradingPartner[][] senderField;
private TradingPartner[][] recipientField;
XSD 从 XML 推断出此处可能有一个数组数组。我假设实际上,您实际上没有这个(我不知道特定的 XML - 这只是我的假设( - 如果是这种情况,那么您只有"一级"数组,因此您需要手动更正为:
public partial class DPDocumentTradingPartnerDirectory {
private TradingPartner[] senderField;
private TradingPartner[] recipientField;
但是一旦你完成了这个 - 反序列化就可以完美地工作,你几乎不需要任何时间就将你的XML文件转换为C#!
试试看:无效的 XML ;)检查开始和结束,您会发现并非所有标签都已关闭。