繁体   English   中英

linq-to-xml问题

[英]Problem with 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)
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中任何位置,因此需要进行调整。 我通常建议您将每个项目拉一次并检查是否为空,而不是像我在这里所做的那样拉动Regions 3次。

您的元素名称与您所截取的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();

您要求元素作为对象,而不是它的价值。 您的代码应为:

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

请注意,此代码非常脆弱,因为如果缺少一个体面元素之一,则会发生异常。 您应该进行一些微调,以获得所需的结果。

抱歉,此答案尚未完成。 您可以使用下面的代码在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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM