我希望linq将我的xml保存在csv中,但我没有问题。
这里有这个括号,因为如果没有它,代码就不会显示(为什么?)
<results>
<Countries country="Albania">
<Regions region="Centralna Albania">
<Provinces province="Durres i okolice">
<Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
<IndividualFlagsWithForObjects Status="1" />
<IndividualFlagsWithForObjects Status="0" />
<IndividualFlagsWithForObjects magazyn="2" />
</Cities>
</Provinces>
</Regions>
</Countries>
<Countries country="Albania">
<Regions region="Centralna Albania">
<Provinces province="Durres i okolice">
<Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
<IndividualFlagsWithForObjects storage="0" Status="1" />
<IndividualFlagsWithForObjects storage="1" Status="0" />
<IndividualFlagsWithForObjects storage="2" Status="1" />
</Cities>
</Provinces>
</Regions>
</Countries>
</results>我必须指出一件重要的事情:父节点是,但是当我使用它的结果(“loaded.Descendants”)时,它没有给我任何东西。
XDocument loaded = XDocument.Load(@"c:\citiesxml.xml");
// create a writer and open the file
TextWriter tw = new StreamWriter("c:\\XmltoCSV.txt");
// Query the data and write out a subset of contacts
var contacts = (from c in loaded.Descendants("Countries")
select new
{
Country = (string)c.Attribute("ountry").Value,
Region = (string)c.Element("Regions").Attribute("region").Value,
Province= c.Element("Regions").Element("Provinces").Attribute("prowincja").Value,
City= c.Element("Regions").Element("Provinces").Element("Cities").Attribute("city").Value,
Kod = c.Element("Regions").Element("Provinces").Element("Cities").Attribute("cityCode").Value,
IndywidualnaFlagaStatus = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("Status"),
IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")
}).ToList();最后一个问题:
IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")给了我:
IndywidualnaFlagaWartosc = {storage="0"} (I see this while debugging)发布于 2010-05-07 20:52:11
var contacts = (from c in loaded.Descendants("Countries")
select new
{
Country = (string)c.Attribute("Country").Value,
Region = (string)c.Element("Regions").Attribute("region").Value,
Province = (string)c.Element("Regions").Element("Provinces").Attribute("province").Value,
City = (string)c.Element("Regions").Element("Provinces").Element("Cities").Attribute("city").Value,
Hotel = (string)c.Element("Hotels").Attribute("hotel").Value
}).ToList();酒店不在您的xml anywhere中,因此需要进行调整。我通常建议你拉取每个项目一次并检查是否为空,而不是像我在这里所做的那样拉取区域3次。
发布于 2010-05-07 20:25:52
您的元素名称与您的xml片段不匹配(片段中的所有元素都是单数,而在linq查询中它们是复数(countries - country、regions - region等)。
var contacts = (from c in loaded.Descendants("Countries")
select new
{
Country = c.Element("Countries").Value,
Region = c.Element("Regions").Value,
Province= c.Element("Provinces").Value,
City = c.Element("Cities").Value,
Hotel = c.Element("Hotels").Value
}).ToList();发布于 2010-05-07 20:25:59
您请求的元素是对象,而不是它的值。你的代码应该是:
var contacts = (from c in loaded.Descendants("Countries")
select new
{
Country = c.Element("Country").Value,
Region = c.Element("region").Value,
Province= c.Element("province").Value,
City = c.Element("city").Value,
Hotel = c.Element("hotel").Value
}).ToList();但是如果我看一下您的XML,我不确定这是否也会给出任何结果。我猜这应该会给你想要的结果:
var contacts = (from c in loaded.Descendants("Countries")
select new
{
Country = c.Attribute("country").Value,
Region = c.Descendants("Regions").FirstOrDefault().Attribute("region")Value,
Province= c.Descendants("Provinces").FirstOrDefault().Attribute("province").Value,
City = c.Descendants("Cities").FirstOrDefault().Attribute("city").Value,
Hotel = c.Descendants("Hotels").FirstOrDefault().Attribute("hotel").Value
}).ToList();请注意,此代码非常脆弱,因为如果缺少一个分面元素,就会发生异常。你应该对自己进行一些微调,以获得你想要的结果。
https://stackoverflow.com/questions/2788445
复制相似问题