LINQ XML 将不同层次结构读入 1 个对象

发布于 2024-08-20 05:09:54 字数 1512 浏览 6 评论 0原文

我有一个 XML 文件

<searchResponse requestID=“500” status=“success”>
    <pso>
        <psoID ID=“770e8400-e29b-41d4-a716-446655448549”
        targetID=“mezeoAccount”/>
        <data>
            <email>[email protected]</email>
            <quotaMeg>100</quotaMeg>
            <quotaUsed>23</quotaUsed>
            <realm>Mezeo</realm>
            <path>/san1/</path>
            <billing>user2</billing>
            <active>true</active>
            <unlocked>true</unlocked>
            <allowPublic>true</allowPublic>
            <bandwidthQuota>1000000000</bandwidthQuota>
            <billingDay>1</billingDay>
        </data>
    </pso>
</searchRequest>

,我想将数据提取到单个业务对象中。 我是否最好

MezeoAccount mcspAccount = new MezeoAccount();
mcspAccount.PsoID = doc.Element("psoID").Attribute("ID").Value;
mcspAccount.Email = doc.Element("email").Value;
...

即使我知道文件中只有 1 条记录,

var psoQuery = from pso in doc.Descendants("data")
    select new MezeoAccount {
        PsoID = pso.Parent.Element("psoID").Attribute("ID").Value,
        Email = pso.Element("email").Value,
        ... };

还是建立一个列表?如果我错过了什么,人们会建议什么是更正确的方法,甚至是更好的方法。

I have an XML file

<searchResponse requestID=“500” status=“success”>
    <pso>
        <psoID ID=“770e8400-e29b-41d4-a716-446655448549”
        targetID=“mezeoAccount”/>
        <data>
            <email>[email protected]</email>
            <quotaMeg>100</quotaMeg>
            <quotaUsed>23</quotaUsed>
            <realm>Mezeo</realm>
            <path>/san1/</path>
            <billing>user2</billing>
            <active>true</active>
            <unlocked>true</unlocked>
            <allowPublic>true</allowPublic>
            <bandwidthQuota>1000000000</bandwidthQuota>
            <billingDay>1</billingDay>
        </data>
    </pso>
</searchRequest>

and I want to extract the data into a single business object. Am I better to go

MezeoAccount mcspAccount = new MezeoAccount();
mcspAccount.PsoID = doc.Element("psoID").Attribute("ID").Value;
mcspAccount.Email = doc.Element("email").Value;
...

or build a list even though I know there is only 1 record in the file?

var psoQuery = from pso in doc.Descendants("data")
    select new MezeoAccount {
        PsoID = pso.Parent.Element("psoID").Attribute("ID").Value,
        Email = pso.Element("email").Value,
        ... };

What would people suggest would be the more correct way, or a better way even, if I missed something.

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

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

发布评论

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

评论(2

金兰素衣 2024-08-27 05:09:54

如果您知道 xml 仅包含一条数据记录,则不应为其创建列表。所以你的第一个例子看起来不错。

我个人使用的模式是这样的:

public class MezeoAccount 
{
    public string PsoID { get; set; }
    public string Email { get; set; }

    public static MezeoAccount CreateFromXml(XmlDocument xml)
    {
        return new MezeoAccount() 
        {
            PsoID = xml.Element("psoID").Attribute("ID").Value,
            Email = doc.Element("email").Value;
        };
    }
}

//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);

If you know that your xml only will contain one record of the data in mind you shouldn't create a list for it. So your first example looks fine.

A pattern I personally use is something like this:

public class MezeoAccount 
{
    public string PsoID { get; set; }
    public string Email { get; set; }

    public static MezeoAccount CreateFromXml(XmlDocument xml)
    {
        return new MezeoAccount() 
        {
            PsoID = xml.Element("psoID").Attribute("ID").Value,
            Email = doc.Element("email").Value;
        };
    }
}

//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);
失而复得 2024-08-27 05:09:54

您似乎没有得到这个问题的有效答案。假设 XML 文件中只能有一个帐户,我会这样做:

using System;
using System.Linq;
using System.Xml.Linq;

public class MezeoAccount
{
    public string PsoId { get; set; }
    public string Email { get; set; }
    public int QuotaMeg { get; set; }
    // Other properties...
}

public class Program
{
    public static void Main()
    {
        XDocument doc = XDocument.Load("input.xml");
        XElement pso = doc.Element("searchResponse").Element("pso");
        XElement data = pso.Element("data");
        MezeoAccount x = new MezeoAccount
        {
            PsoId = pso.Element("psoID").Attribute("ID").Value,
            Email = data.Element("email").Value,
            QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
            // Other properties...
        };
    }
}

It looks like you didn't get a working answer to this question. Assuming that there can only be one account in the XML file, I would do it like this:

using System;
using System.Linq;
using System.Xml.Linq;

public class MezeoAccount
{
    public string PsoId { get; set; }
    public string Email { get; set; }
    public int QuotaMeg { get; set; }
    // Other properties...
}

public class Program
{
    public static void Main()
    {
        XDocument doc = XDocument.Load("input.xml");
        XElement pso = doc.Element("searchResponse").Element("pso");
        XElement data = pso.Element("data");
        MezeoAccount x = new MezeoAccount
        {
            PsoId = pso.Element("psoID").Attribute("ID").Value,
            Email = data.Element("email").Value,
            QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
            // Other properties...
        };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文