开发者

Getting the value of XML tag xith XMLReader and add to LinkedList C#

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 an开发者_JS百科d 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();
        }
    }


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;
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜