初始化一个从“编辑”、“特殊粘贴”、“将XML粘贴为类”中获取的XML类。

6
我可以协助翻译。这段内容涉及编程,讲述了通过“编辑,粘贴特殊内容,将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;
        }
    }
}

}

我不知道如何使用来自文件的数据初始化这个类。

2个回答

7

反序列化是读取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);          
}

0

你现在很可能是个专家了...(几年后,一个新手回答) 我找不到如何使用Paste Special XML的解决方案,我一直得到“对象引用未设置为对象实例”的错误。这是因为我的XML有一个属性和一个元素(我正在使用VB)。我意识到VS没有初始化变量[Name]Field。所以我只需为[Name][Name]类添加As New。在你的情况下,是New ItemsItemPosition。


迟做总比不做好:)8年后的两周前,我回到了这个问题,因为我再次需要它,现在你写了另一个答案,这太棒了。 - Ferri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接