LINQ inserting XML RRS feed

  • Question

  • I have a string that is the result of a serialization of an object. It is basically XML. I have a table that has a schema that is defined by the following LINQ to SQL object:

    	public partial class OrderForecast : INotifyPropertyChanging, INotifyPropertyChanged
    		private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    		private string _Identifier;
    		private string _Category;
    		private string _SubCategory;
    		private byte _IdentifierType;
    		private string _Model;
    		private System.DateTime _Created;
    		private System.Xml.Linq.XElement _ModelDetail;

    To insert a new row I do:

                    OrderForecast row = new OrderForecast();
                    row.Identifier = id;
                    row.Category = category;
                    row.SubCategory = subcategory;
                    row.IdentifierType = identifiertype;
                    row.ModelDetail = modeldetail;

    The problem is the line 'row.ModelDetail = modeldetail'. THe compiler refuses to compile this statement indicating that it cannot convert from a string to a 'System.Xml.Linq.XElement. So the question is how do I convert a string to an XElement or what would be the best way to serialize an object into an XElement? (because there will be alot of these items)

    Thank you.


    Friday, May 22, 2009 5:02 PM


All replies

  • Try this:

    OrderForecast row = new OrderForecast() 
        Identifier = id;
        Category = category;
        SubCategory = subcategory;
        IdentifierType = identifiertype;
        ModelDetail = modeldetail.Value;

    adding a .Value to get the elements value (if it is a single node).

    William Wegerson (www.OmegaCoder.Com )
    Friday, May 22, 2009 5:18 PM
  • The problem is that 'modeldetail' as I have listed it is a string and there isn't a 'Value' method on a string. Did you think that modeldetail was something different?

    Thank you.

    Friday, May 22, 2009 5:24 PM
  • row.ModeDetail = XElement.Parse(modeldetail);
    MVP XML My blog
    • Marked as answer by KevinBurton Friday, May 22, 2009 5:32 PM
    Friday, May 22, 2009 5:32 PM
  • I get the following error:

    Error 1 The name 'XElement' does not exist in the current context

    Friday, May 22, 2009 5:34 PM
  • Either spell it out in that line e.g.
    row.ModelDetail = System.Xml.Linq.XElement.Parse(modeldetail);
    or better add a
    using System.Xml.Linq;
    add the beginning of your code file.

    MVP XML My blog
    Friday, May 22, 2009 5:42 PM