1

I have a C# MVC application which I need to output the following data to a view:

            <versions>
              <product>true</product>
              <type>city</type>
              <factory name="Demme" url="http://test1.com" thumbnail="http://test3.com/img1" interval="10" />
              <factory name="Vollick" url="http://test2.com" thumbnail="http://test3.com/img1" interval="10" />
              <factory name="Tony" url="http://test3.com" thumbnail="http://test3.com/img1" interval="10" />
            </versions>

The above data comes from a SQL table/column which stores the data as a XML data type. Can somone give me a code example to extract the values of the elements(maybe assign each value to variable) so I can pass it to a view?

So I need to get the values "true" , "City", "Demme" , "http://test1.com", "http://test3.com/img1....and so on.

Whats the best way to present this data to a view?

6
  • Do you have a specific problem with parsing this xml or have you not even tried learning how to do it? whathaveyoutried.com Commented Jul 1, 2012 at 4:59
  • You can't extract values from XML in C#. It's impossible. You need to use C or Java :) PS: Have you tried anything yet? Do you have any code you can show? Did "C# XML" turn up zero hits on Google??? Commented Jul 1, 2012 at 5:01
  • is this a different question from your other one with the same data structure? Commented Jul 1, 2012 at 5:04
  • 1
    XML was canceled in 2002, so your data is stuck. Commented Jul 1, 2012 at 5:04
  • possible duplicate of C#: How to extract data from SQL XML data type field and assign to enums Commented Jul 1, 2012 at 5:05

2 Answers 2

1

My idea is to create classes, corresponding to your Xml file, a Version class, a Factory class. Load the xml file, and then pass it to your class that return your data, here is how I do it :

The Version class :

public class Version
{
    public bool IsProduct { get; set; }
    public string City { get; set; }
    public List<Factory> Factories { get; set; }

    //Create a version
    public Version(XElement xVersion)
    {
        IsProduct = Convert.ToBoolean(xVersion.Element("Product").Value);
        City = xVersion.Element("City").Value;
        Factories = Factory.GetFactories(xVersion);
    }

    //Get the list of versions
    public static List<Version> GetVersions(XElement xDocument)
    {
        if (xDocument == null)
            return null;

        List<Version> list = new List<Version>();
        var xVersions = xDocument.Elements("Version");

        foreach (var xVersion in xVersions)
        {
            list.Add(new Version(xVersion));
        }

        return list;
    }
}

The Factory class :

public class Factory
{
    public string Name { get; set; }
    public string Url { get; set; }
    public string Thumbnail { get; set; }
    public string Interval { get; set; }

    //Create a factory
    public Factory(XElement xFactory)
    {
        Name = xFactory.Attribute("Name").Value;
        Url = xFactory.Attribute("Url").Value;
        Thumbnail = xFactory.Attribute("Thumbnail").Value;
        Interval = xFactory.Attribute("Interval").Value;
    }

    //Get the factories of a version
    public static List<Factory> GetFactories(XElement xVersion)
    {
        var xFactories = xVersion.Elements("Factory");
        if (xFactories == null)
            return null;

        List<Factory> list = new List<Factory>();

        foreach (var xFactory in xFactories)
        {
            list.Add(new Factory(xFactory));
        }

        return list;
    }
}

And last, in your MCV Controller :

private void myMethod()
    {
        var xDocument = XElement.Load("XmlFilePath");
        var versions = Version.GetVersions(xDocument);

        //And then, pass the -versions- to your typed view ^^
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for you help. I appreciate it. Implementing this now
I am almost there but one problem. The XML data is coming from a SQL database and not a file so XElement.Load("XmlFilePath"); throws and error "Illegal Characters in path.
I used XElement.Parse instead of XElement.Load and I am no longer getting an error. Let me see if this works now.
I debugged my program and I can actually see the data in thexdocument passed to Getversion();however var xVersions = xDocument.Elements("Version") is null
Does your Xml file contain only one element "Versions" ? if so, use xDocument.Element("Versions") instead. An other thing, be sure of the element name("Versions" or "Version"), this is why the data is null I think
0
using System.Xml;
    List<string> values= new List<string>();    
    XmlTextReader reader = new XmlTextReader ("books.xml");
    while (reader.Read()) 
    {
           switch (reader.NodeType) 
           {
                   while (reader.MoveToNextAttribute()) // Read the attributes.
                     values.add(reader.Value);
                   break;
         case XmlNodeType.Text: //Display the text in each element.
                     values.add(reader.Value);
                   break;
         case XmlNodeType. EndElement: //Display the end of the element.
          Console.WriteLine(">");
                   break;
           }
       }

Now you have a list of values. Assign it to the model and then use the model to populate the view.

Comments

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.