C# LINQ XML将不同的层次结构读入一个对象
我有一个XML文件C# LINQ XML将不同的层次结构读入一个对象,c#,linq-to-xml,C#,Linq To Xml,我有一个XML文件 <searchResponse requestID=“500” status=“success”> <pso> <psoID ID=“770e8400-e29b-41d4-a716-446655448549” targetID=“mezeoAccount”/> <data> <email>user2@example.net</ema
<searchResponse requestID=“500” status=“success”>
<pso>
<psoID ID=“770e8400-e29b-41d4-a716-446655448549”
targetID=“mezeoAccount”/>
<data>
<email>user2@example.net</email>
<quotaMeg>100</quotaMeg>
<quotaUsed>23</quotaUsed>
<realm>Mezeo</realm>
<path>/san1/</path>
<billing>user2</billing>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
</data>
</pso>
</searchRequest>
或者创建一个列表,即使我知道文件中只有一条记录
var psoQuery = from pso in doc.Descendants("data")
select new MezeoAccount {
PsoID = pso.Parent.Element("psoID").Attribute("ID").Value,
Email = pso.Element("email").Value,
... };
如果我遗漏了一些内容,人们会建议采用更正确的方法,甚至更好的方法。如果您知道您的xml只包含一条数据记录,那么您不应该为它创建列表。所以你的第一个例子看起来不错 我个人使用的模式如下:
public class MezeoAccount
{
public string PsoID { get; set; }
public string Email { get; set; }
public static MezeoAccount CreateFromXml(XmlDocument xml)
{
return new MezeoAccount()
{
PsoID = xml.Element("psoID").Attribute("ID").Value,
Email = doc.Element("email").Value;
};
}
}
//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);
using System;
using System.Linq;
using System.Xml.Linq;
public class MezeoAccount
{
public string PsoId { get; set; }
public string Email { get; set; }
public int QuotaMeg { get; set; }
// Other properties...
}
public class Program
{
public static void Main()
{
XDocument doc = XDocument.Load("input.xml");
XElement pso = doc.Element("searchResponse").Element("pso");
XElement data = pso.Element("data");
MezeoAccount x = new MezeoAccount
{
PsoId = pso.Element("psoID").Attribute("ID").Value,
Email = data.Element("email").Value,
QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
// Other properties...
};
}
}
看起来你没有得到这个问题的有效答案。假设XML文件中只能有一个帐户,我会这样做:
public class MezeoAccount
{
public string PsoID { get; set; }
public string Email { get; set; }
public static MezeoAccount CreateFromXml(XmlDocument xml)
{
return new MezeoAccount()
{
PsoID = xml.Element("psoID").Attribute("ID").Value,
Email = doc.Element("email").Value;
};
}
}
//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);
using System;
using System.Linq;
using System.Xml.Linq;
public class MezeoAccount
{
public string PsoId { get; set; }
public string Email { get; set; }
public int QuotaMeg { get; set; }
// Other properties...
}
public class Program
{
public static void Main()
{
XDocument doc = XDocument.Load("input.xml");
XElement pso = doc.Element("searchResponse").Element("pso");
XElement data = pso.Element("data");
MezeoAccount x = new MezeoAccount
{
PsoId = pso.Element("psoID").Attribute("ID").Value,
Email = data.Element("email").Value,
QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
// Other properties...
};
}
}
XML输入上的模式是什么?具体来说,
searchResponse/pso/data
的元素是必需的还是可以省略?如果可以忽略它们,它们是否有默认值?但是,第二个选项实际上只是语法糖,因此不必重复地重新编写变量名……而且您可以在任何地方使用该语法糖,而不仅仅是在LINQ查询中。杰斯珀的回答是这样的。你还在寻找这个问题的答案吗?也许如果你先解决这个问题,你的另一个问题会更容易解决。我已经发布了一个答案,希望对您有所帮助。另外请注意,您的XML文件中有几个错误:开始标记没有匹配的结束标记,以及一些无效字符:“and”。