0

Having a hard time retrieving an attribute from my XML. I need to grab this attribute, and send and store it. I can't get it to garb the attributes. :( Just need help with the attributes.

<portfolios>
  <portfolio>
    <id>00001</id>
    <investment ticker="ASD">
      <shares>20</shares>
      <price>42.50</price>
    </investment>
  </portfolio>

  <pricedata days="4">
    <stock ticker="ASD">
      <price value="42.50"/>
      <price value="43.50"/>
      <price value="39.00"/>
      <price value="45.00"/>
    </stock>
  </pricedata>
</portfolios>

What I have so far!

public bool readXmlData(String filename)
{
    XDocument document = XDocument.Load(filename);

    foreach (XElement portfolio in document.Descendants("portfolio"))
        {
            XElement id = portfolio.Element("id");
            string id2 = id != null ? id.Value : string.Empty;
            portList.Add(new SmallPortfolio(id2));

            XAttribute ticker = portfolio.Attribute("investment");

            foreach(XElement investment in document.Descendants("investment"))
            {
                XElement shares = investment.Element("shares");
                XElement price =  investment.Element("price");

                temp.Add(new Investment(

                ticker != null ? ticker.Value : string.Empty,
                shares != null ? int.Parse(shares.Value) : default(int),
                price != null ? double.Parse(shares.Value) : default(double)
                ));
            }
        }

    foreach (XElement stock in document.Descendants("pricedata"))
    {
        XAttribute tick = stock.Attribute("stock");
        List<Double> pricetemp2 = new List<Double>();
        foreach (XElement price in document.Descendants("stock"))
        {
            XAttribute value = price.Attribute("price");
            pricetemp2.Add(value.Value);
        }
        groupList.Add(new PriceGroup(tick,pricetemp2));
    }
    return true;
}
public List<SmallPortfolio> getPortfolioList() { return null; }
public List<PriceGroup> getPriceList() { return null; }
}

1 Answer 1

1

<price> is an element, but you are accessing it as if it was an attribute <stock price="..."/>.

Try this:

foreach (XElement stock in document.Descendants("stock"))
{
    string ticker = (string)stock.Attribute("ticker");
    List<Double> pricetemp2 = new List<Double>();
    foreach (XElement price in stock.Descendants("price"))
    {
        double value = (double)price.Attribute("value");
        pricetemp2.Add(value);
    }
    groupList.Add(new PriceGroup(ticker, pricetemp2));
}

Casting XAttribute to double will use the proper XML rules for numbers (XmlConvert.ToDouble). Using double.Parse is incorrect as it uses culture-specific number formatting (e.g. in Germany it expects a decimal comma instead of a decimal point).

Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't it be code foreach (XElement price in document.Descendants("stock"))code Since Price doesn't have any child elements in it?
document.Descendants("price") gives you all the <price> elements (not the non-existent children of all <price> elements). I've updated the answer to show how to handle the parent <stock> elements.
I think I figured it out. Since ticker is an attribute of Investment. I should be able to use XAttribute ticker = portfolio.Attribute("investment"); in the foreach with the shares and price elements?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.