[英]XML to LINQ deserialize to an object from an XML Document
好的,這些命名空間再次讓我感到頭疼。
我想要一個長的XML文檔並將其反序列化為實體對象列表。 在這種情況下,對象僅類似於平面POJO。
XML看起來像:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<QueryResponse xmlns="http://markets.midwestiso.org/dart/xml">
<DayAheadLMP day="2011-02-25">
<PricingNode location="AEC">
<PricingNodeHourly hour="1">
<LMP>23.1</LMP>
<MCC>-0.44</MCC>
<MLC>-0.49</MLC>
<RegMCP>0</RegMCP>
<SpinMCP>0</SpinMCP>
<SuppMCP>0</SuppMCP>
</PricingNodeHourly>
等等等
我已經達到:
var repsonseXML = XDocument.Parse(CallMUI(requestXml, muiUrl));
它將我的XML作為字符串返回,並將其解析為XDocument對象
我想要的列表對象如下所示:
/// <summary>
/// There are no comments for MISO.IR.IntegrationModel.LMP_DayAhead in the schema.
/// </summary>
/// <KeyProperties>
/// Location
/// Interval
/// SyncId
/// </KeyProperties>
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="MISO.IR.IntegrationModel", Name="LMP_DayAhead")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class LMP_DayAhead : global::System.Data.Objects.DataClasses.EntityObject
{
/// <summary>
/// Create a new LMP_DayAhead object.
/// </summary>
/// <param name="location">Initial value of Location.</param>
/// <param name="interval">Initial value of Interval.</param>
/// <param name="lMP">Initial value of LMP.</param>
/// <param name="mLC">Initial value of MLC.</param>
/// <param name="mCC">Initial value of MCC.</param>
/// <param name="syncId">Initial value of SyncId.</param>
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public static LMP_DayAhead CreateLMP_DayAhead(string location, global::System.DateTime interval, decimal lMP, decimal mLC, decimal mCC, int syncId)
{
LMP_DayAhead lMP_DayAhead = new LMP_DayAhead();
lMP_DayAhead.Location = location;
lMP_DayAhead.Interval = interval;
lMP_DayAhead.LMP = lMP;
lMP_DayAhead.MLC = mLC;
lMP_DayAhead.MCC = mCC;
lMP_DayAhead.SyncId = syncId;
return lMP_DayAhead;
}
如何使用所有這些名稱空間完成它?
您將要查看System.Xml.Serialization命名空間。
LMP_DayAhead dayAhead = GetDayAheadData();
var serializer = new XmlSerializer(dayAhead.GetType());
using (var writer = XmlWriter.Create("dayahead.xml"))
{
serializer.Serialize(writer, shoppingCart);
}
在命名空間方面,您的poco必須模仿xml中的繼承關系,我相信有一種方法可以告訴序列化程序忽略命名空間(不記得我的頭了)
我想我知道了:
public static void ProcessDayAheadQuery(string requestXml, int syncEntityId, string muiUrl)
{
var repsonseXML = XDocument.Load(XmlReader.Create(CallMUIStream(requestXml, muiUrl)));
XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns2 = "http://markets.midwestiso.org/dart/xml";
using (var db = new IntLMPDB())
{
SyncJob syncEntity =
(from s in db.SyncJob where s.SyncId == syncEntityId select s).FirstOrDefault();
DateTime rDay = DateTime.Now;
String query = String.Empty;
foreach (XElement xe in repsonseXML.Descendants(ns + "Envelope").FirstOrDefault().Descendants(ns2 + "QueryResponse").Descendants(ns2 + "DayAheadLMP"))
{
rDay = DateTime.Parse(xe.FirstAttribute.Value);
query = xe.Name.LocalName;
// string day = xe.FirstAttribute.Value;
foreach (XElement node in xe.Descendants(ns2 + "PricingNode"))
{
// string location = node.FirstAttribute.Value;
strin
foreach (XElement hourly in node.Descendants(ns2 + "PricingNodeHourly"))
{
//var newRow = new LMP_DayAhead
//{
// Location = node.FirstAttribute.Value,
// Interval = rDay.AddHours(Double.Parse(hourly.FirstAttribute.Value)),
// LMP = decimal.Parse(hourly.Descendants(ns2 + "LMP").FirstOrDefault().Value),
// MLC = decimal.Parse(hourly.Descendants(ns2 + "MLC").FirstOrDefault().Value),
// MCC = decimal.Parse(hourly.Descendants(ns2 + "MCC").FirstOrDefault().Value),
// SyncJob = syncEntity
//};
//db.AddToLMP_DayAhead(newRow);
}
}
}
db.SaveChanges();
UpdateLastDbPost(db, query, rDay);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.