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

从我提出的另一个问题来看,答案几乎让我到达那里,但我遇到了障碍。我需要后代的所有交付,并且它们都一起运行。


我已经尝试过当前的代码(但它只获得第一个交付部分)。


我在下面尝试的方法引起的问题是它不能处理具有多个部分的部分。


我有一个使用数据表的半工作解决方案,但这是一个看起来更干净的解决方案,我真的很想了解如何让它像这样工作。


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


心有法竹
浏览 122回答 2
2回答

一只甜甜圈

不确定您是否真的使用类模型。但我做了一些调整以获得有价值的东西(这会给你更多的数据灵活性)。课程:public class Recipient&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public int RecipientCode { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public RecipientInfo RecipientNameAndAddress { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public IList<RecipientDelivery> Deliveries { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class RecipientInfo&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public RecipientAddress Address { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class RecipientAddress&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Line1 { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string CityTownOrLocality { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string StateOrProvince { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string PostalCode { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class RecipientDelivery&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string DeliveryID { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string DeliveryType { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string DeliveryRoute { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string ToteID { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string NursingStation { get; set; }&nbsp; &nbsp; }然后是工作:&nbsp; &nbsp; var doc = XDocument.Parse(file);&nbsp; &nbsp; XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");&nbsp; &nbsp; XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();&nbsp; &nbsp; var recipients = recipientDeliveries.Descendants(ns0 + "Recipient").ToList();&nbsp; &nbsp; var RecipientList = new List<Recipient>();&nbsp; &nbsp; foreach (var item in recipients)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var deliveries = item.Descendants(ns0 + "Deliveries").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; var deliveriesNodes = deliveries.Descendants(ns0 + "Delivery").ToList();&nbsp; &nbsp; &nbsp; &nbsp; var recipientInfo = item.Descendants(ns0 + "RecipientNameAndAddress").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; var recipientAddress = recipientInfo.Descendants(ns0 + "Address").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; var deliverList = new List<RecipientDelivery>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var del in deliveriesNodes)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var delivery = new RecipientDelivery()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeliveryID = del.Element(ns0 + "DeliveryID").Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeliveryType = del.Element(ns0 + "DeliveryType").Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeliveryRoute = del.Element(ns0 + "DeliveryRoute").Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ToteID = del.Element(ns0 + "ToteID").Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NursingStation = del.Element(ns0 + "NursingStation").Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deliverList.Add(delivery);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var recipient = new Recipient()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RecipientCode = Convert.ToInt32(item.Element(ns0 + "RecipientCode").Value),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RecipientNameAndAddress = new RecipientInfo()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = recipientInfo.Element(ns0 + "Name").Value.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Address = new RecipientAddress()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Line1 = recipientAddress.Element(ns0 + "Line1").Value.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CityTownOrLocality = recipientAddress.Element(ns0 + "CityTownOrLocality").Value.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StateOrProvince = recipientAddress.Element(ns0 + "StateOrProvince").Value.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PostalCode = recipientAddress.Element(ns0 + "PostalCode").Value.ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Deliveries = deliverList&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; RecipientList.Add(recipient);&nbsp; &nbsp; }然后整个收件人都会在 中RecipientList,您可以使用它。

婷婷同学_

对之前结果的一个小修改:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;namespace ConsoleApplication1{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XNamespace ns0 = doc.Root.GetNamespaceOfPrefix("ns0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement sender = doc.Descendants(ns0 + "SenderNameAndAddress").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] senderAddress = sender.Descendants(ns0 + "Address").Elements().Select(x => (string)x).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement recipientDeliveries = doc.Descendants(ns0 + "RecipientDeliveries").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var results = recipientDeliveries.Elements(ns0 + "Recipient").Select(x => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = (string)x.Descendants(ns0 + "Name").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address = x.Descendants(ns0 + "Address").Elements().Select(y => (string)y).ToArray(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deliveries = x.Descendants(ns0 + "Delivery").Select(y => new {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deliveryID = (string)y.Descendants(ns0 + "DeliveryID").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deliveryType = (string)y.Descendants(ns0 + "DeliveryType").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deliveryRoute = (string)y.Descendants(ns0 + "DeliveryRoute").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toteID = (string)y.Descendants(ns0 + "ToteID").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nursingStation = (string)y.Descendants(ns0 + "NursingStation").FirstOrDefault()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP