views:

184

answers:

3

I have the following XML file:

<?xml version="1.0" encoding="utf-8" ?>
<scripts>
  <ScriptName>
    <name>
        "My Name"
    </ScriptName>
    <ScriptBody>
        "body contents"
    </ScriptBody>
  </script>
</scripts>

And the following object:

    public class DbScript
{
    #region Constructors
    public DbScript()
        : this(string.Empty, string.Empty)
    {
    }
    public DbScript(string name, string body)
    {
        ScriptName = name;
        ScriptBody = body;
    }
    #endregion
    #region Properties
    /// <summary>
    /// Script name
    /// </summary>
    public string ScriptName { get; set; }
    /// <summary>
    /// Script body
    /// </summary>
    public string ScriptBody { get; set; }
    #endregion
}

What is the quickest way to populate the collection of DBScript object from the contents of the XML file? Should I look into serializers?

Thanks!

+2  A: 

Assuming you meant to type the XML syntax like so:

<?xml version="1.0" encoding="utf-8" ?>
<scripts>
  <script>
 <ScriptName>
  "My Name"
 </ScriptName>
 <ScriptBody>
  "body contents"
 </ScriptBody>
  </script>
</scripts>

Then this code should parse it. Untested.

var doc = XDocument.Load("input.xml");
var x = from script in doc.Descendents("scripts").Descendents("script")
  select new {
   Name = script.Element("ScriptName").Value,
   Body = script.Element("ScriptBody").Value
  };
Nathan Taylor
+2  A: 
    var document = XElement.Load(fileName);
    var collection = from elem in document.Elements("script")
                     select new DBScript(
                                  elem.Element("ScriptName").Value,
                                  elem.Element("ScriptBody").Value
                                );

will do the trick for ya

Rune FS
+2  A: 

hello,

Using .Net Serialization is absolutely my preference, in this case you would deserialize the file to an object. If you take your xml (which I edited a bit):

<?xml version="1.0" encoding="utf-8" ?>
<scripts>
  <script>
    <ScriptName>
        "My Name"
    </ScriptName>
    <ScriptBody>
        "body contents"
    </ScriptBody>
  </script>
</scripts>

Then you create some classes the represents the xml:

public class Scripts
{

  /// <summary>
  /// only allow xml serializer to create instances.
  /// </summary>
  private Scripts()
  {        
  }
  [XmlElement]
  public List<script> script{ get; set; }
}
public class script
{
  public script()
  {
  }

  [XmlElement]
  public string ScriptName { get; set; }

  [XmlElement]
  public string ScriptBody{ get; set; }
}

And then when that is all set up correctly you can deserialize the file:

string xmlFilePath = "THE_PATH_TO_THE_XML";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Scripts));
using (XmlReader xmlReader = XmlReader.Create(xmlFilePath))
{
    Scripts scripts = (Scripts)xmlSerializer.Deserialize(xmlReader);
}

Xml serialization is really powerful, check it out in the docs: http://msdn.microsoft.com/en-us/library/ms950721.aspx.

robb

Robb C
-1 for using `XmlTextReader` instead of `XmlReader.Create`, and for not implementing a `using` block.
John Saunders
Yep XmlReader.Create is the way to go, didnt realize that. And, good catch on the using statement...
Robb C