linq-to-xml的问题

时间:2010-05-07 12:17:53

标签: c# xml linq-to-xml

我想通过linq在csv中保存我的xml,我有问题。

这个括号在这里没有它这个代码没有显示(为什么?)

<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>

我必须指出一件重要的事情: 父节点是 但是当我使用它时,装载了它。后代(“结果”)它什么都没给我。

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)

4 个答案:

答案 0 :(得分:4)

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中,因此需要进行调整。我通常会建议您拉一次每个项目并检查空值,而不是像我在这里那样拉动区域3次。

答案 1 :(得分:1)

Yout元素名称与您的xml剪辑不匹配(片段中的所有元素都是单数,在您的linq查询中它们是复数(国家/地区,国家/地区,区域等)。

 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();

答案 2 :(得分:1)

您正在请求元素作为对象,而不是它的值。你的代码应该是:

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();

请注意,此代码非常脆弱,因为如果缺少其中一个正面元素,则会发生异常。你应该做一些微调,以获得你想要的结果。

答案 3 :(得分:1)

对不起,这个答案还不完整。除了具有以下代码的Country之外,你不会得到任何值,但它应该是一个很好的起点,所以尝试使用c.Element(),你应该像这样使用c.Attribute():

var contacts = (from c in loaded.Descendants("Countries")
    select new
    {
        Country = (string)c.Attribute("country"),
        Region = (string)c.Attribute("region"),
        Province = (string)c.Attribute("province"),
        City = (string)c.Attribute("city"),
        Hotel = (string)c.Attribute("hotel")
    }).ToList();