asp.net 初始化从“编辑”、“选择性粘贴”、“将XML粘贴为类”中获取的xml类

62lalag4  于 2023-01-27  发布在  .NET
关注(0)|答案(2)|浏览(209)

我有一个从"编辑、选择性粘贴、将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;
        }
    }
}

}
我不知道如何用文件中的数据初始化这个类。

ajsxfq5m

ajsxfq5m1#

反序列化是阅读XML文档并构造该文档的XML架构(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);          
}
rsl1atfo

rsl1atfo2#

你现在应该是Maven了...(几年后,一个新手回答)我找不到如何使用选择性粘贴XML的解决方案,我得到了“对象引用没有设置为对象的示例”。这是当我的XML有一个属性和一个元素时(我使用VB)。我意识到VS没有初始化变量[Name]Field。所以我只是为[Name][Name]类添加了As New。在您的情况下,是New ItemsItemPosition。

相关问题