通过 XMLReader 获取 XML 标签 x 的值并添加到 LinkedList C#

发布于 2024-10-06 10:36:26 字数 2320 浏览 3 评论 0原文

我是编程新手,遇到了严重的问题并且无法摆脱它。

我有 5 个 XML URL。例如 http://www.shopandmiles.com/xml/3_119_3.xml

这是一个 XML URL,我必须获取值并将其写入相关列中的数据库。 我的列名称和 XML 标记名称确实匹配。

当我编写下面的代码时,阅读器元素缺少 null xml 值。有些标签内部没有价值。我必须将它们添加到链接列表中,因为在该代码之后,我将遍历链接列表,但如果我无法为 null xml 值添加值,则顺序不匹配。所以列名和里面的数据不匹配。我失去了订单。我的所有代码都在这里,您也可以查看代码中的注释是否有帮助。谢谢大家。

public void WebServiceShopMilesCampaignsXMLRead(string URL)
    {
        XmlReader reader = XmlReader.Create(URL);
        LinkedList<string> linkedList = new LinkedList<string>();

        List<ShopAndMilesCampaigns> shopMileCampaigns = new List<ShopAndMilesCampaigns>();

        try
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        linkedList.AddLast(reader.Value);
                        break;
                }
            }
        }

        catch (XmlException exception)
        {
            Console.WriteLine("XML okurken bir sorun oluştu, hata detayı --> " + exception.Message);
        }

        LinkedListNode<string> node = linkedList.First;

        while (node != null)
        {
            ShopAndMilesCampaigns shopMilesCampaign = new ShopAndMilesCampaigns();

            shopMilesCampaign.Name = node.Value; // Null values mixes up the order because i cant add as null with reader.read above
            node = node.Next;
            shopMilesCampaign.Summary = node.Value;
            node = node.Next;
            shopMilesCampaign.AccountName = node.Value;
            node = node.Next;
            shopMilesCampaign.Category = node.Value;
            node = node.Next;
            shopMilesCampaign.Sector = node.Value;
            node = node.Next;
            shopMilesCampaign.Details = node.Value;
            node = node.Next;
            shopMilesCampaign.Image = node.Value;
            node = node.Next;
            shopMilesCampaign.Status = 1;
            node = node.Next;

            shopMileCampaigns.Add(shopMilesCampaign);
        }

        foreach (ShopAndMilesCampaigns shopMileCampaign in shopMileCampaigns)
        {
            shopMileCampaign.Insert();
        }
    }

I am a new to programming, and have a serious problem and cant get out of it.

I have 5 XML URLs. such as http://www.shopandmiles.com/xml/3_119_3.xml

This is an XML URL which I have to get values and write to database in related columns.
My column names and XML tag names do match.

When I write the below code, reader element miss null xml values. Some tags do not have value inside. I have to add them null to linkedlist because after that code, i am going through the linked list but the order doesnt match if ı cant add a value for null xml values. So column names and data inside doesnt match. i lose the order. My all code is here, you can also check comment in the code if that helps. Thank you all.

public void WebServiceShopMilesCampaignsXMLRead(string URL)
    {
        XmlReader reader = XmlReader.Create(URL);
        LinkedList<string> linkedList = new LinkedList<string>();

        List<ShopAndMilesCampaigns> shopMileCampaigns = new List<ShopAndMilesCampaigns>();

        try
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        linkedList.AddLast(reader.Value);
                        break;
                }
            }
        }

        catch (XmlException exception)
        {
            Console.WriteLine("XML okurken bir sorun oluştu, hata detayı --> " + exception.Message);
        }

        LinkedListNode<string> node = linkedList.First;

        while (node != null)
        {
            ShopAndMilesCampaigns shopMilesCampaign = new ShopAndMilesCampaigns();

            shopMilesCampaign.Name = node.Value; // Null values mixes up the order because i cant add as null with reader.read above
            node = node.Next;
            shopMilesCampaign.Summary = node.Value;
            node = node.Next;
            shopMilesCampaign.AccountName = node.Value;
            node = node.Next;
            shopMilesCampaign.Category = node.Value;
            node = node.Next;
            shopMilesCampaign.Sector = node.Value;
            node = node.Next;
            shopMilesCampaign.Details = node.Value;
            node = node.Next;
            shopMilesCampaign.Image = node.Value;
            node = node.Next;
            shopMilesCampaign.Status = 1;
            node = node.Next;

            shopMileCampaigns.Add(shopMilesCampaign);
        }

        foreach (ShopAndMilesCampaigns shopMileCampaign in shopMileCampaigns)
        {
            shopMileCampaign.Insert();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青春如此纠结 2024-10-13 10:36:27

我找到了答案。在此,特意向您通报一下。

如果 XmlNodeType 等于 Element,则循环继续从 XML 数据中读取并查找 XML 标记的 Whitesapces 和结束 Element。下面的代码给出了 XML 标签的确切值,即使它是空的。

public LinkedList<string> AddToLinkedList(XmlReader reader)
    {

        LinkedList<string> linkedList = new LinkedList<string>();

        try
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        reader.Read();
                    Start:
                        if (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.Element)
                        {
                            reader.Read();
                            goto Start;
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            linkedList.AddLast("");
                        }
                        else
                        {
                            linkedList.AddLast(reader.Value);
                        }
                        break;
                }
            }
        }

I found the answer. Here it is to let you know.

If the XmlNodeType is equal to Element, then the loop continues to read from the XML data and looks for Whitesapces and end Element of XML tag. The below code gives me the exact value of XML tag even it is empty.

public LinkedList<string> AddToLinkedList(XmlReader reader)
    {

        LinkedList<string> linkedList = new LinkedList<string>();

        try
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        reader.Read();
                    Start:
                        if (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.Element)
                        {
                            reader.Read();
                            goto Start;
                        }
                        else if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            linkedList.AddLast("");
                        }
                        else
                        {
                            linkedList.AddLast(reader.Value);
                        }
                        break;
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文