为了账号安全,请及时绑定邮箱和手机立即绑定

分解 XML 文件中的所有元素 C#

分解 XML 文件中的所有元素 C#

C#
心有法竹 2023-07-22 15:59:01
从我提出的另一个问题来看,答案几乎让我到达那里,但我遇到了障碍。我需要后代的所有交付,并且它们都一起运行。我已经尝试过当前的代码(但它只获得第一个交付部分)。我在下面尝试的方法引起的问题是它不能处理具有多个部分的部分。我有一个使用数据表的半工作解决方案,但这是一个看起来更干净的解决方案,我真的很想了解如何让它像这样工作。var document = XDocument.Parse(xmlText);            var doc = XDocument.Parse(xmlText);            XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");            XElement sender = doc.Descendants(ns0 + "SenderNameAndAddress").FirstOrDefault();            string[] senderAddress = sender.Descendants(ns0 + "Address").Elements().Select(x => (string)x).ToArray();            XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();            var results = recipientDeliveries.Elements(ns0 + "Recipient").Select(x => new            {                recipientCode = ((string)x.Descendants(ns0 + "RecipientCode").FirstOrDefault()),                name = (string)x.Descendants(ns0 + "Name").FirstOrDefault(),                address = x.Descendants(ns0 + "Address").Elements().Select(y => (string)y).ToArray(),                deliveries = x.Descendants(ns0 + "Deliveries").Elements().Select(y => (string)y).ToArray(),                deliveryID = (string)x.Descendants(ns0 + "DeliveryID").FirstOrDefault(),                deliveryType = (string)x.Descendants(ns0 + "DeliveryType").FirstOrDefault(),                deliveryRoute = (string)x.Descendants(ns0 + "DeliveryRoute").FirstOrDefault(),                toteID = (string)x.Descendants(ns0 + "ToteID").FirstOrDefault(),                nursingStation = (string)x.Descendants(ns0 + "NursingStation").FirstOrDefault()            }).ToList();
查看完整描述

2 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

不确定您是否真的使用类模型。但我做了一些调整以获得有价值的东西(这会给你更多的数据灵活性)。


课程:


public class Recipient


    {

        public int RecipientCode { get; set; }

        public RecipientInfo RecipientNameAndAddress { get; set; }

        public IList<RecipientDelivery> Deliveries { get; set; }

    }


    public class RecipientInfo

    {

        public string Name { get; set; }

        public RecipientAddress Address { get; set; }


    }

    public class RecipientAddress

    {

        public string Line1 { get; set; }


        public string CityTownOrLocality { get; set; }


        public string StateOrProvince { get; set; }


        public string PostalCode { get; set; }

    }

    public class RecipientDelivery

    {

        public string DeliveryID { get; set; }


        public string DeliveryType { get; set; }


        public string DeliveryRoute { get; set; }


        public string ToteID { get; set; }

        public string NursingStation { get; set; }


    }

然后是工作:


    var doc = XDocument.Parse(file);


    XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");


    XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();


    var recipients = recipientDeliveries.Descendants(ns0 + "Recipient").ToList();


    var RecipientList = new List<Recipient>();



    foreach (var item in recipients)

    {

        var deliveries = item.Descendants(ns0 + "Deliveries").FirstOrDefault();


        var deliveriesNodes = deliveries.Descendants(ns0 + "Delivery").ToList();


        var recipientInfo = item.Descendants(ns0 + "RecipientNameAndAddress").FirstOrDefault();


        var recipientAddress = recipientInfo.Descendants(ns0 + "Address").FirstOrDefault();


        var deliverList = new List<RecipientDelivery>();



        foreach (var del in deliveriesNodes)

        {

            var delivery = new RecipientDelivery()

            {

                DeliveryID = del.Element(ns0 + "DeliveryID").Value,

                DeliveryType = del.Element(ns0 + "DeliveryType").Value,

                DeliveryRoute = del.Element(ns0 + "DeliveryRoute").Value,

                ToteID = del.Element(ns0 + "ToteID").Value,

                NursingStation = del.Element(ns0 + "NursingStation").Value

            };


            deliverList.Add(delivery);

        }


        var recipient = new Recipient()

        {

            RecipientCode = Convert.ToInt32(item.Element(ns0 + "RecipientCode").Value),


            RecipientNameAndAddress = new RecipientInfo()

            {

                Name = recipientInfo.Element(ns0 + "Name").Value.ToString(),


                Address = new RecipientAddress()

                {

                    Line1 = recipientAddress.Element(ns0 + "Line1").Value.ToString(),

                    CityTownOrLocality = recipientAddress.Element(ns0 + "CityTownOrLocality").Value.ToString(),

                    StateOrProvince = recipientAddress.Element(ns0 + "StateOrProvince").Value.ToString(),

                    PostalCode = recipientAddress.Element(ns0 + "PostalCode").Value.ToString()

                },


            }, 


            Deliveries = deliverList

        };


        RecipientList.Add(recipient);

    }

然后整个收件人都会在 中RecipientList,您可以使用它。


查看完整回答
反对 回复 2023-07-22
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

对之前结果的一个小修改:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.Xml.Linq;


namespace ConsoleApplication1

{

    class Program

    {

        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)

        {

            XDocument doc = XDocument.Load(FILENAME);

            XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");


            XElement sender = doc.Descendants(ns0 + "SenderNameAndAddress").FirstOrDefault();

            string[] senderAddress = sender.Descendants(ns0 + "Address").Elements().Select(x => (string)x).ToArray();


            XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();


            var results = recipientDeliveries.Elements(ns0 + "Recipient").Select(x => new

            {

                name = (string)x.Descendants(ns0 + "Name").FirstOrDefault(),

                address = x.Descendants(ns0 + "Address").Elements().Select(y => (string)y).ToArray(),

                deliveries = x.Descendants(ns0 + "Delivery").Select(y => new {

                    deliveryID = (string)y.Descendants(ns0 + "DeliveryID").FirstOrDefault(),

                    deliveryType = (string)y.Descendants(ns0 + "DeliveryType").FirstOrDefault(),

                    deliveryRoute = (string)y.Descendants(ns0 + "DeliveryRoute").FirstOrDefault(),

                    toteID = (string)y.Descendants(ns0 + "ToteID").FirstOrDefault(),

                    nursingStation = (string)y.Descendants(ns0 + "NursingStation").FirstOrDefault()

                }).ToList()

            }).ToList();

        }

    }

}


查看完整回答
反对 回复 2023-07-22
  • 2 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信