C# 获取节点下的多个属性值
我很难解析同时具有属性和子体的节点 我需要进入customer节点并将其下的所有内容解析为一个值,以便在网页上使用C# 获取节点下的多个属性值,c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,我很难解析同时具有属性和子体的节点 我需要进入customer节点并将其下的所有内容解析为一个值,以便在网页上使用 var contacts = from c in xdoc.Descendants("contact") select new Contact { Email = (string)c.Element("email"),
var contacts = from c in xdoc.Descendants("contact")
select new Contact
{
Email = (string)c.Element("email"),
HomePhone = (string)c.Element("phone").Attribute("type") // ?
};
和我的XML:
<customer>
<contact>
<name part="first">Rick</name>
<name part="last">Smith</name>
<email>rick@yahoo.com</email>
<phone type="home">2299998989</phone>
<phone type="work">2298887878</phone>
<phone type="cell">2297778878</phone>
<address type="home">
<street line="1">4001 Coleman Rd</street>
<street line="2">Ste. 99</street>
<city>Tempe</city>
<regioncode>AZ</regioncode>
<postalcode>43444</postalcode>
</address>
</contact>
<comments>Customer comments</comments>
</customer>
<vendor>
<contact>
<name part="full" type="salesperson">Joe Smith</name>
</contact>
</vendor>
瑞克
史密斯
rick@yahoo.com
2299998989
2298887878
2297778878
科尔曼路4001号
Ste。99
坦佩
阿兹
43444
客户意见
乔-史密斯
要获得家庭电话号码,您应该使用:
HomePhone = (string)c.Elements("phone").SingleOrDefault(e => (string)e.Attribute("type") == "home");
要获取家庭电话号码,您应该使用:
HomePhone = (string)c.Elements("phone").SingleOrDefault(e => (string)e.Attribute("type") == "home");
您需要以下类来保存已解析的数据:
public class Customer
{
public Contact Contact { get; set; }
public string Comments { get; set; }
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string HomePhone { get; set; }
public string WorkPhone { get; set; }
public string CellPhone { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Type { get; set; }
public string StreetLine1 { get; set; }
public string StreetLine2 { get; set; }
public string City { get; set; }
public string RegionCode { get; set; }
public string PostalCode { get; set; }
}
您需要以下类来保存已解析的数据:
public class Customer
{
public Contact Contact { get; set; }
public string Comments { get; set; }
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string HomePhone { get; set; }
public string WorkPhone { get; set; }
public string CellPhone { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Type { get; set; }
public string StreetLine1 { get; set; }
public string StreetLine2 { get; set; }
public string City { get; set; }
public string RegionCode { get; set; }
public string PostalCode { get; set; }
}
你犯了什么错误?你的代码似乎还可以,只是不完整。你得到了什么错误?你的代码看起来还行,只是它不完整,哥们..这太棒了,你做得很快!!我有大约一个小时的工作价值来完成这个项目…将支付,给我发电子邮件cherrywood1000@gmail.com关于细节,这是石头,你很快就搞定了!!我有大约一个小时的工作价值来完成这个项目…将支付,给我发电子邮件cherrywood1000@gmail.com详情