3

This is my XML feed. It has more than this.

 <Cproducts>
   <ID>001</ID>
   <Name>name one</Name>
   <Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
        <Departure>
          <Date>2015-12-06T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
    <Availability>
 </Cproducts>  

In my case I want to go through all available dates.that means when user select a date[from the form], then according to that date ,other data should provide.to do that I have to look through all available dates.how can I do that. this is my code in MVC.

public void getDatas(string destination, string cruisetype, string datetime)
{
    XElement rootele = XElement.Load(Server.MapPath("~/XmlFiles/CruiseData/cruiseprodutstwo.xml"));

    var selecttoDate = rootele.Elements("Cproducts").Elements("Availability").Elements("Departure").Elements("Date"); //here I want to get the data that the [date == datetime]
  • I honestly do not understand why this question gets three upvotes :-( – Uwe Keim Dec 07 '15 at 07:44
  • Can you please tell me is there a direct way to look that the specific date is available.as an example below, ` var getneededData = rootele.Elements("CruiseProduct") .Where(l => l.Element("Location").Value == destination );` using this we can check through whole document and gets data that Location equals to given string. like that I want to search through whole dates. Is there a way to do this mr @Uwe Keim. help me with this sir . –  Dec 07 '15 at 08:48

2 Answers2

1
    static void Main(string[] args)
    {
        string xml = @"<Cproducts>   
<Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
        <Departure>
          <Date>2015-12-06T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
    </Availability>
 </Cproducts>";

        XDocument doc = XDocument.Parse(xml);

        var list = (from element in doc.Elements("Cproducts").Elements("Availability").Elements("Departure")
                    where Convert.ToDateTime(element.Element("Date").Value) < DateTime.Now
                    select element).ToList();

        foreach(XElement el in list)
        {
            Console.WriteLine(el.Element("Date").Value);
        }

    }

It is not understandable what exactly you want to do. So I wrote you a way to select the dates from your xml where they are < from Today. In your case you should not use XDocument.Parse, you need XDocument.Load(uri). Also next time at least try to add valid xml.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
1

You can probably create a seralization for your XML like this

[XmlRoot(ElementName="Price")]
public class Price {
    [XmlElement(ElementName="Type")]
    public string Type { get; set; }
    [XmlElement(ElementName="Value")]
    public string Value { get; set; }
    [XmlElement(ElementName="Qty")]
    public string Qty { get; set; }
}

[XmlRoot(ElementName="Pricing")]
public class Pricing {
    [XmlElement(ElementName="Price")]
    public List<Price> Price { get; set; }
}

[XmlRoot(ElementName="Departure")]
public class Departure {
    [XmlElement(ElementName="Date")]
    public string Date { get; set; }
    [XmlElement(ElementName="Pricing")]
    public Pricing Pricing { get; set; }
}

[XmlRoot(ElementName="Availability")]
public class Availability {
    [XmlElement(ElementName="Departure")]
    public List<Departure> Departure { get; set; }
}

[XmlRoot(ElementName="Cproducts")]
public class Cproducts {
    [XmlElement(ElementName="Availability")]
    public Availability Availability { get; set; }
}

Which would benefit you to get the ObservableCollection of the Objects for your XML and it would be much more easier to query any complex data as well. So in the current scenario.

You can create a ObservableCollection of Cproducts after the Serialization and then can query using LINQ.

ObservableCollection<Cproducts> ResultantCollection = new ObservableCollection<Cproducts>();
if(File.Exists(path + "\\YourXML.xml"))
{
    XElement root = XElement.Load(path + "\\YourXML.xml");
    root.Element("Cproducts").Elements("Availability").All<XElement>(xe =>
    {
        ResultantCollection.AddAvailability(Availability av);
        return true;
    });

The class will be defined like this

public class CproductsColl : ObservableCollection<Cproducts>
{
    public User AddAvailability(Availability av)
    {
        base.Add(av);
        return av;
    }
}       
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • If it is okay, can you show me how to create ObservableCollection of Cproducts. because I'm new to this feild –  Dec 07 '15 at 06:00
  • I have no idea to go forward ,should I add to a list or what . please help me with this @Mohit –  Dec 07 '15 at 07:09
  • `XElement cElement = XElement.Load(Server.MapPath("~/XmlFiles/CruiseData/cruiseprodutstwo.xml")); var getdataa = cElement.Elements("CruiseProduct"); ObservableCollection ResultantCollection = new ObservableCollection();` –  Dec 07 '15 at 07:12
  • @anu: You should go thru [ObservableCollection tutorial](http://stackoverflow.com/questions/4450582/observablecollection-tutorial) and [Youtube: Observable Collection in C# Demo](https://www.youtube.com/watch?v=ISwIiOmgMCc) and lemme know if i can help u. – Mohit S Dec 07 '15 at 07:13
  • @anu: Its not a smaller concept to write with the answer but the links provided will have more links which will definitely let u have the idea how to implement ObservableCollections and makes your jobs quicker and easier. – Mohit S Dec 07 '15 at 07:17
  • thanx for the comment ,but what I want is how to add return xml to ResultantCollection –  Dec 07 '15 at 07:30