Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在模型中设置XML属性_C#_Xml_Serialization_Model_Xml Attribute - Fatal编程技术网

C# 在模型中设置XML属性

C# 在模型中设置XML属性,c#,xml,serialization,model,xml-attribute,C#,Xml,Serialization,Model,Xml Attribute,我需要在XML中设置一个属性。我要求: <finAccount version="1.00"> 我已经看过几个在上面的方法中设置属性的例子,但是我在整个应用程序中都使用这个方法。有没有办法在模型中设置xml属性(version=“1.00)?试试这个。类应该是XmlRoot,数组应该是XmlElement。XmlElement避免在代码中添加两层相同的标记。如果不使用XmlElement,您会看到区别 [XmlRoot("finPOWERConnect")] public clas

我需要在XML中设置一个属性。我要求:

<finAccount version="1.00">

我已经看过几个在上面的方法中设置属性的例子,但是我在整个应用程序中都使用这个方法。有没有办法在模型中设置xml属性(version=“1.00)?

试试这个。类应该是XmlRoot,数组应该是XmlElement。XmlElement避免在代码中添加两层相同的标记。如果不使用XmlElement,您会看到区别

[XmlRoot("finPOWERConnect")]
public class ApplicationData
{
    [XmlElement("finAccount")]
    public List<Account> Accounts {get; set; }
}

[XmlRoot("finAccount")]
public class Account
{
    [XmlAttribute("version")]
    public string Version { get; set; } 
    //Account stuff
}
​
[XmlRoot(“finPOWERConnect”)]
公共类应用程序数据
{
[XmlElement(“财务账户”)]
公共列表帐户{get;set;}
}
[XmlRoot(“财务账户”)]
公共类帐户
{
[XmlAttribute(“版本”)]
公共字符串版本{get;set;}
//会计资料
}
​

您能澄清一下您的模型吗?1)您的
应用程序数据
类不可编译,因为它只能应用于属性、索引器、字段、参数或返回。2)什么是
财务帐户
,以及它与
应用程序数据
的关系是什么?@dbc我在一个示例中访问了XmlAttribute,并在一个attem中添加了它我也修复了上面的模型。这有帮助吗?Thx。所以你有一个
帐户
类的列表,所有这些类都需要有一个属性
“version”
,它总是“1.00”(因此在XML中重复了很多次)?太好了!!非常感谢
 public Boolean SerialiseObjectToXmlString(Object obj, ref string xml)
    {
        System.IO.MemoryStream ms = null;
        bool Ok = true;
        XmlSerializer xmlSerializer = null;

        xml = "";

        //Serialise
        try
        {
            xmlSerializer = new XmlSerializer(obj.GetType());
            ms = new MemoryStream();
            xmlSerializer.Serialize(ms, obj);
            xml = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
            ms.Close();
        }

        catch(Exception ex)
        {
            Ok = false;
            xml = ex.Message;
        }

        finally
        {
            if (ms != null) ms.Dispose();
        }

        return Ok;
    }
[XmlRoot("finPOWERConnect")]
public class ApplicationData
{
    [XmlElement("finAccount")]
    public List<Account> Accounts {get; set; }
}

[XmlRoot("finAccount")]
public class Account
{
    [XmlAttribute("version")]
    public string Version { get; set; } 
    //Account stuff
}
​