linq-to-xml 的问题
我想通过 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("results")时它什么也没有给我。
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)
I want by linq save my xml in csv and I have o problem.
This bracket are here beacuse without it this code is not displaying (why ? )
<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>
I must point one important thing:
the parent node is
but when I use it loaded.Descendants("results") it gives me nothing.
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();
last problem:
IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")
gives me :
IndywidualnaFlagaWartosc = {storage="0"} (I see this while debugging)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Hotel 不在您的 xml 中,因此需要进行调整。我通常建议您将每个项目拉取一次并检查空值,而不是像我在这里所做的那样拉取区域 3 次。
Hotel is not in your xml anywhere so that will need to be adjusted. I would generally recommend that you pull each item once and check for nulls, instead of pulling Regions 3 times as I have done here.
Yout 元素名称与您的 xml 片段不匹配(片段中的所有元素都是单数,而在 linq 查询中它们是复数(国家 - 国家 、地区 - 地区 等)。
Yout element names don't match your xml snipped (all your elements in the snippet are singular and in your linq query they are plural (countries - country , regions - region etc).
您请求元素作为对象,而不是它的值。您的代码应该是:
但如果我查看您的 XML,我不确定这是否也会给出任何结果。我猜这应该会给你想要的结果:
请注意,这段代码非常脆弱,因为如果缺少一个正向元素,就会发生异常。您应该对自己进行一些微调以获得您想要的结果。
You're requesting the Element as object, and not it's value. Your code should be:
But I'm not sure this gives any results as well if I look at your XML. I'm guessing this should give you the results you want:
Please note that this code is quite fragile, because if one of the decentant elements is missing, an Exception will occur. You should some fine-tuning yourself to get the results you want.
抱歉,这个答案并不完整。使用下面的代码,除了 Country 之外,您不会获得任何值,但这应该是一个很好的起点,因此尝试使用 c.Element() 并且您应该像这样使用 c.Attribute() :
I'm sorry, this answer isn't complete. You don't get values in anything but Country with the code below, but it should be a good starting point, so try using c.Element() and you should be using c.Attribute() like so: