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,您可以使用它。
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();
}
}
}
- 2 回答
- 0 关注
- 121 浏览
添加回答
举报