2011-12-19 71 views
0
<?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#類是我爲XMLXML到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#類

我不能夠在一次單一鏡頭中將其轉換爲c#DPDocument對象,我嘗試了所有方法,但是我沒有實現這一目標。 最後我遇到了一個最糟糕的解決方案,比如通過單個節點列表並最終處理該列表,該方法對我有用。但我需要一個優雅的解決方案來解決我的問題。 預先感謝:)

回答

1

試試這個:NOT VALID XML;)檢查開始和結束,你會發現並非所有的標籤都被關閉。

+0

是的。 ''標籤沒有關閉。 – Strelok 2011-12-19 05:53:32

+0

不是唯一的;)其實還有更多。因此沒有辦法通過標準的XML機制來處理這個文檔。 – TomTom 2011-12-19 05:55:42

+0

如果你發現任何標籤沒有關閉,對不起,這是我的錯誤,但我的XML文件看起來只有這樣,它格式良好。如果你有任何想法轉換一個PLZ給我寶貴的解決方案給我,謝謝:) – Ramakrishna 2011-12-19 07:13:46

2

當你有一個有效的XML文檔,你可以做一個兩個步驟:

  1. 運行得到了.NET框架安裝到您的XML轉換成XSD的xsd.exe命令行工具( XML模式):

    c:\> xsd.exe yourfile.xml 
    

    這將創建一個在你所需要的第二步相同的位置稱爲yourfile.xsd文件。

  2. 再次運行xsd.exe命令行工具,XML模式轉換成C#代碼:

    c:\> xsd.exe /c yourfile.xsd 
    

    這將創建一個在同一位置稱爲yourfile.cs文件,其中包含你所需要的C#類和輔助結構反序列化你正在處理的這個特定的XML。現在

,與C#文件,你應該能夠做到:

XmlSerializer serializer = new XmlSerializer(typeof(DPDocument)); 
TextReader tr = new StreamReader(@"path-to-your-XML-file-here"); 
var result = serializer.Deserialize(tr); 

,你應該讓你的DPDocument類從XML文件反序列化。

在這個具體的例子,試圖做到這一點的時候,你會絆倒的錯誤 - 這是因爲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#中幾乎沒有任何時間!

+0

爲什麼不直接從XML到C#? (實際上,我可以想出一些答案,但我很好奇你的想法是什麼) – sq33G 2011-12-19 07:05:42

+0

我試過TextReader tr = null; XmlSerializer serializer2 = null; DPDocument dpDocument = null; serializer2 = new XmlSerializer(typeof(DPDocument),「DPDocument」); tr = new StreamReader(@「E:\ Consumption_Forecast_SA1854_24 - Copy.xml」); dpDocument =(DPDocument)serializer2.Deserialize(tr);但我無法轉換。 – Ramakrishna 2011-12-19 07:18:22

+0

@ sq33G:怎麼樣? XSD工具在XML-> XSD和XSD-> CS上工作,我不認爲你可以直接從XML到C#。手動完成所有這些都是一件混亂而費時的事情...... – 2011-12-19 07:31:01