Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a xml class obtained from Edit, Paste Special, Paste XML as Classes

I have a class generated from "Edit, Paste Special, Paste XML as Classes." like explains here. Generating Data Type Classes from XML

XML:

<?xml version="1.0"?>
<Items version="1.0">
  <Item InputFileName="G:\FileFile.txt">
    <Position X="500" Y="100" Z="150"/>
  </Item>
</Items>

Class:

namespace Produccion.ClassFile
{
/// <comentarios/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Items
{

    private ItemsItem[] itemField;

    private decimal versionField;

    /// <comentarios/>
    [System.Xml.Serialization.XmlElementAttribute("Item")]
    public ItemsItem[] Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal version
    {
        get
        {
            return this.versionField;
        }
        set
        {
            this.versionField = value;
        }
    }
}

/// <comentarios/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ItemsItem
{

    private ItemsItemPosition positionField;

    private string inputFileNameField;

    /// <comentarios/>
    public ItemsItemPosition Position
    {
        get
        {
            return this.positionField;
        }
        set
        {
            this.positionField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string InputFileName
    {
        get
        {
            return this.inputFileNameField;
        }
        set
        {
            this.inputFileNameField = value;
        }
    }
}

/// <comentarios/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ItemsItemPosition
{

    private decimal xField;

    private decimal yField;

    private decimal zField;

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal X
    {
        get
        {
            return this.xField;
        }
        set
        {
            this.xField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Y
    {
        get
        {
            return this.yField;
        }
        set
        {
            this.yField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Z
    {
        get
        {
            return this.zField;
        }
        set
        {
            this.zField = value;
        }
    }
}

}

I don't know how I can initialize this class with data from file.

like image 827
Ferri Avatar asked Jul 30 '14 15:07

Ferri


People also ask

How do I paste a class into JSON?

Web Essentials Download page. Create empty class file in VS. Copy valid JSON text into clipboard. You will now see the "Paste Json as Classes" under Edit -> Paste Special -> Paste Json as Classes.

How do I Paste Special in Visual Studio?

Developer Community After additional searching around, the "Paste Special" option is only visible once additional components are installed (For some reason). If anyone else comes across this, go back to the Visual Studio Installer -> Tick "ASP.NET and web development", and install that.


1 Answers

Deserialization is the process of reading an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document.

You would do something like this

XmlSerializer serializer = new XmlSerializer(typeof(Items));

// Declare an object variable of the type to be deserialized.
Items i;

using (Stream reader = new FileStream(filename, FileMode.Open))
{
    // Call the Deserialize method to restore the object's state.
    i = (Items)serializer.Deserialize(reader);          
}
like image 181
Sergey Avatar answered Nov 03 '22 19:11

Sergey