[英]Read XMl using Linq returning empty object
我正在嘗試讀取xml
樣本XML
<customer-list>
<customer>
<FirstName>B</FirstName>
<LastName>C</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>9056953000</Phone>
<Address1></Address1>
<Address2>70 East Beaver Creek Rd</Address2>
<City>Richmond Hill</City>
<State>ON</State>
<ZipCode>L4B3B2</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
<customer>
<FirstName>P</FirstName>
<LastName>M</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>7045955246</Phone>
<Address1>Residence Inn</Address1>
<Address2>55 Minthorn Blvd</Address2>
<City>Markham</City>
<State>ON</State>
<ZipCode>L3T7Y9</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
</customer-list>
讀碼
public void ReadXml(string path)
{
var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")
select new
{
FirstName = (string)node.Attribute("FirstName").Value,
LastName = (string)node.Attribute("LastName").Value,
Email = (string)node.Attribute("Email").Value,
OptInEmail = (string)node.Attribute("OptInEmail").Value,
Phone = (string)node.Attribute("Phone").Value,
Address1 = (string)node.Attribute("Address1").Value,
Address2 = (string)node.Attribute("Address2").Value,
City = (string)node.Attribute("City").Value,
State = (string)node.Attribute("State").Value,
ZipCode = (string)node.Attribute("ZipCode").Value,
CountryCode = (string)node.Attribute("CountryCode").Value
};
foreach (var item in customer)
{
var test = item.FirstName;
}
}
但是每次我嘗試訪問它時,Cutomer都是空的。 沒有從xml讀取值? 任何建議...
根據leo建議更新了代碼。
public void ReadXml(string path)
{
var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")
select new
{
FirstName = node.Element("FirstName"),
LastName = node.Element("LastName"),
Email = node.Element("Email"),
OptInEmail = node.Element("OptInEmail"),
Phone = node.Element("Phone"),
Address1 = node.Element("Address1"),
Address2 = node.Element("Address2"),
City = node.Element("City"),
State = node.Element("State"),
ZipCode = node.Element("ZipCode"),
CountryCode = node.Element("CountryCode")
};
foreach (var item in customer)
{
var test = item.FirstName;
}
}
不工作。
根據salem22建議更新了代碼。 沒有業務規則或邏輯返回。閱讀完foreach循環后,它將無法運行。
public void ReadXml(string path)
{
var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")
select new
{
FirstName = node.Element("FirstName").Value,
LastName = node.Element("LastName").Value,
Email = node.Element("Email").Value,
OptInEmail = node.Element("OptInEmail").Value,
Phone = node.Element("Phone").Value,
Address1 = node.Element("Address1").Value,
Address2 = node.Element("Address2").Value,
City = node.Element("City").Value,
State = node.Element("State").Value,
ZipCode = node.Element("ZipCode").Value,
CountryCode = node.Element("CountryCode").Value
};
foreach (var item in customer)
{
var test = item.FirstName;
}
}
添加在xdoc
閱讀后看到的實際文件
<customers xmlns="http://www.demandware.com/xml/impex/customer/2007-05-31">
<customer-list>
<customer>
<FirstName>B</FirstName>
<LastName>C</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>9056953000</Phone>
<Address1>On</Address1>
<Address2>70 East Beaver Creek Rd</Address2>
<City>Richmond Hill</City>
<State>ON</State>
<ZipCode>L4B3B2</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
<customer>
<FirstName>P</FirstName>
<LastName>M</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>7045955246</Phone>
<Address1>Residence Inn</Address1>
<Address2>55 Minthorn Blvd</Address2>
<City>Markham</City>
<State>ON</State>
<ZipCode>L3T7Y9</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
</customer-list>
</customers>
嘗試這個:
XDocument xd=Xdocument.Load(path);
XNamespace ns = "http://www.demandware.com/xml/impex/customer/2007-05-31";
var customer=from node in xd.Descendants(ns+"customer")
select new
{
FirstName=node.Element(ns+"FirstName").Value,
...
};
您的Elements
不是Attributes
。 使用XElement.Element(string)
方法
FirstName = (string)node.Element("FirstName"),
LastName = (string)node.Element("LastName"),
...
如果要進行顯式強制轉換,也不要使用Value
屬性。 沒有用。 Value
已經是一個字符串,如果找不到該元素,它將引發異常。
您需要使用Element
更改對Attribute
的調用,因為您的customer
沒有任何屬性,並且您對獲取內部元素的文本感興趣。 替換所有與以下方法類似的方法調用...
FirstName = (string)node.Attribute("FirstName").Value,
有了這個...
FirstName = node.Element("FirstName").Value,
您一無所獲,因為沒有任何要讀取的屬性。元素“ customer”是帶有子元素(firstname,lastname等)的complexType元素。 為了讀取它們的值,您必須將方法從node.Attribute更改為node.Element。 使用您發布的XML,我運行了以下代碼並獲得了預期的結果。
var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")
select new
{
FirstName = node.Element("FirstName").Value.ToString(),
LastName = node.Element("LastName").Value.ToString(),
Email = node.Element("Email").Value.ToString(),
foreach (var item in customer)
{
var test = item.FirstName;
Console.WriteLine(test);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.