初始化从编辑、粘贴特殊、粘贴xml为类中获得的xml类

本文关键字:xml 编辑 初始化 粘贴 | 更新日期: 2023-09-27 18:15:44

我有一个从"编辑,粘贴特殊,粘贴XML为类"中生成的类。从XML生成数据类型类

XML:

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

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;
        }
    }
}

}

我不知道如何用file中的data初始化这个类

初始化从编辑、粘贴特殊、粘贴xml为类中获得的xml类

反序列化是读取XML文档并构造一个与文档的XML Schema (XSD)强类型的对象的过程。

你可以这样做

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);          
}

你现在可能是专家了…(几年后,一个新手回答)我找不到如何使用Paste Special XML的解决方案,我得到的是"对象引用没有设置为对象的实例"。那时我的XML有一个属性和一个元素(我使用的是VB)。我意识到VS没有初始化变量[Name]Field。所以我刚刚为[Name][Name]类添加了As New。在您的示例中,New ItemsItemPosition.