XML 到 LINQ 从 XML 文档反序列化为对象

发布于 2024-10-19 15:52:16 字数 2600 浏览 1 评论 0原文

好吧,这些命名空间再次让我感到困惑。

我想要获取一个很长的 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;
    }

如何使用所有这些命名空间来完成它?

OKay this is just over my head again with these namespaces.

I want to take a long XML document and deserialize it down into a list of entity objects. The objects in this case resemble just flat POJOs.

The XML looks like:

<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>

etc etc..

i have gotten as far as:

var repsonseXML = XDocument.Parse(CallMUI(requestXml, muiUrl));

Which returns my XML as a string and Parses it into the XDocument object

the list objects I want look like:

    /// <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;
    }

How do I get it done with all those namespaces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-10-26 15:52:16

您需要查看 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 中的继承,并且我相信有一种方法可以告诉序列化器忽略命名空间(我记不清了)

Your going to want to look into the System.Xml.Serialization namespace.

LMP_DayAhead dayAhead = GetDayAheadData();
var serializer = new XmlSerializer(dayAhead.GetType());
using (var writer = XmlWriter.Create("dayahead.xml"))
{
    serializer.Serialize(writer, shoppingCart);
}

in terms of the namespaces your poco's will have to mimic the inheritance found in the xml, and i believe there is a way of telling the serializer to ignore the namespaces (can't remember off the top of my head)

睫毛溺水了 2024-10-26 15:52:16

我想我已经明白了:

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);
    } 
}

I think I figured it out:

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);
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文