linq-to-xml 的问题

发布于 2024-09-01 01:23:42 字数 2750 浏览 4 评论 0原文

我想通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-09-08 01:23:42
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();

Hotel 不在您的 xml 中,因此需要进行调整。我通常建议您将每个项目拉取一次并检查空值,而不是像我在这里所做的那样拉取区域 3 次。

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

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.

╭ゆ眷念 2024-09-08 01:23:42

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

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

 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();
江南烟雨〆相思醉 2024-09-08 01:23:42

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

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

请注意,这段代码非常脆弱,因为如果缺少一个正向元素,就会发生异常。您应该对自己进行一些微调以获得您想要的结果。

You're requesting the Element as object, and not it's value. Your code should be:

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

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:

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

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.

我不在是我 2024-09-08 01:23:42

抱歉,这个答案并不完整。使用下面的代码,除了 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();

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:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文