来自类的 C# 调用列表
本文关键字:调用 列表 | 更新日期: 2023-09-27 18:30:44
我目前正在提取一个httpweb请求以获取保修信息。我已经测试了每个部分的信息,它有效。一旦我把它放进一个类,它就失败了。
我的问题是,如何有效地调用设备类?
Dell computerRequest = new Dell(apiKey);
XDocument xlTest = new XDocument();
xlTest = computerRequest.XmlResponse(txtTag.Text.Trim());
XNamespace getAssetWarrantyA = "http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset";
XNamespace getAssetWarrantyI = "http://www.w3.org/2001/XMLSchema-instance";
List<Device> xlAsset = (from asset in xlTest.Descendants(getAssetWarrantyA + "Response")
select new Device()
{
DeviceInfo = (from defaultInfo in asset.Descendants(getAssetWarrantyA + "DellAsset")
select new DeviceBase()
{
Product = (string)asset.Descendants(getAssetWarrantyA + "MachineDescription").FirstOrDefault(),
OrderNumber = (string)asset.Descendants(getAssetWarrantyA + "OrderNumber").FirstOrDefault(),
ServiceTag = (string)asset.Descendants(getAssetWarrantyA + "ServiceTag").FirstOrDefault(),
ShipDate = (string)asset.Descendants(getAssetWarrantyA + "ShipDate").FirstOrDefault()
}).ToList(),
WarrantyInfo = (from warranty in asset.Descendants(getAssetWarrantyA + "Warranty")
select new Warranty()
{
Service = (string)warranty.Descendants(getAssetWarrantyA + "ServiceLevelDescription").FirstOrDefault(),
Provider = (string)warranty.Descendants(getAssetWarrantyA + "ServiceProvider").FirstOrDefault(),
StartDate = (string)warranty.Descendants(getAssetWarrantyA + "StartDate").FirstOrDefault(),
EndDate = (string)warranty.Descendants(getAssetWarrantyA + "EndDate").FirstOrDefault(),
TypeOfWarranty = (string)warranty.Descendants(getAssetWarrantyA + "EntitlementType").FirstOrDefault()
}).ToList(),
}).ToList();
**//FAILS -- No object set to reference**
Device testDevice = new Device();
MessageBox.Show(testDevice.WarrantyInfo.Count.ToString());
dtGrdWar.DataSource = xlAsset.ToArray();
}
public class Device
{
public List<DeviceBase> DeviceInfo{ get; set; }
public List<Warranty> WarrantyInfo { get; set; }
}
public class DeviceBase
{
public string Product { get; set; }
public string OrderNumber { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
public class Warranty
{
public string Service { get; set; }
public string Provider { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string TypeOfWarranty { get; set; }
}
.XML:
<GetAssetWarrantyResponse xmlns="http://tempuri.org/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<GetAssetWarrantyResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset">
<a:Faults/>
<a:Response>
<a:DellAsset>
<a:AssetParts i:nil="true"/>
<a:CountryLookupCode>11</a:CountryLookupCode>
<a:CustomerNumber>117731304</a:CustomerNumber>
<a:IsDuplicate>false</a:IsDuplicate>
<a:ItemClassCode>OC002</a:ItemClassCode>
<a:LocalChannel>66</a:LocalChannel>
<a:MachineDescription>COMPELLENT SC8000,1st,2nd,UPG</a:MachineDescription>
<a:OrderNumber>NANANANAN</a:OrderNumber>
<a:ParentServiceTag i:nil="true"/>
<a:ServiceTag>XXXXXXX</a:ServiceTag>
<a:ShipDate>2014-02-07T00:00:00</a:ShipDate>
<a:Warranties>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5366</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>974-9929</a:ItemNumber>
<a:ServiceLevelCode>ZL</a:ServiceLevelCode>
<a:ServiceLevelDescription>CML - Storage Center Core Base</a:ServiceLevelDescription>
<a:ServiceLevelGroup>11</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5406</a:ItemNumber>
<a:ServiceLevelCode>SV</a:ServiceLevelCode>
<a:ServiceLevelDescription>Silver Premium Support</a:ServiceLevelDescription>
<a:ServiceLevelGroup>8</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5416</a:ItemNumber>
<a:ServiceLevelCode>SV</a:ServiceLevelCode>
<a:ServiceLevelDescription>Silver Premium Support</a:ServiceLevelDescription>
<a:ServiceLevelGroup>8</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5376</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>975-0042</a:ItemNumber>
<a:ServiceLevelCode>ZL</a:ServiceLevelCode>
<a:ServiceLevelDescription>CML - Storage Center Core Base</a:ServiceLevelDescription>
<a:ServiceLevelGroup>11</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2014-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5376</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2014-02-07T00:00:00</a:StartDate></a:Warranty></a:Warranties></a:DellAsset></a:Response></GetAssetWarrantyResult></GetAssetWarrantyResponse>
问题在于正确创建类。以下是任何好奇的人的更正:
public class Device
{
private List<DeviceBase> _deviceInfo = new List<DeviceBase>();
private List<Warranty> _warrantyInfo = new List<Warranty>();
public List<DeviceBase> DeviceInfo
{
get
{ return _deviceInfo; }
set
{ _deviceInfo = value; }
}
public List<Warranty> WarrantyInfo
{
get
{ return _warrantyInfo; }
set
{ _warrantyInfo = value; }
}
}
public class DeviceBase
{
public string Product { get; set; }
public string OrderNumber { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
public class Warranty
{
public string Service { get; set; }
public string Provider { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string TypeOfWarranty { get; set; }
}