Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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# XmlSerializer-将属性成员序列化为属性_C#_Xml_Properties_Serialization - Fatal编程技术网

C# XmlSerializer-将属性成员序列化为属性

C# XmlSerializer-将属性成员序列化为属性,c#,xml,properties,serialization,C#,Xml,Properties,Serialization,我正在尝试将内验证属性的所有成员序列化为属性(XmlSerializer) 两个类: public class Tree { public Point Location { get; set; } } public class AppleTree : Tree { [XmlAttribute] public int FruitCount { get; set; } } AppleTree appleTree = new AppleTree { Location

我正在尝试将内验证属性的所有成员序列化为属性(
XmlSerializer

两个类:

public class Tree
{
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }
}
AppleTree appleTree = new AppleTree
{
    Location = new Point(10, 20),
    FruitCount = 69
};
// Serialize ...
<?xml version="1.0"?>
<AppleTree FruitCount="69">
  <Location>
    <X>10</X>
    <Y>20</Y>
  </Location>
</AppleTree>
<?xml version="1.0"?>
<AppleTree FruitCount="69" X="10" Y="20" />
public class Tree
{
    [XmlIgnore]
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }

    [XmlAttribute]
    public int X
    {
        get { return Location.X; }
        set { Location = new Point(value, Location.Y); }
    }

    [XmlAttribute]
    public int Y
    {
        get { return Location.Y; }
        set { Location = new Point(Location.X, value); }
    }
}
主程序:

public class Tree
{
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }
}
AppleTree appleTree = new AppleTree
{
    Location = new Point(10, 20),
    FruitCount = 69
};
// Serialize ...
<?xml version="1.0"?>
<AppleTree FruitCount="69">
  <Location>
    <X>10</X>
    <Y>20</Y>
  </Location>
</AppleTree>
<?xml version="1.0"?>
<AppleTree FruitCount="69" X="10" Y="20" />
public class Tree
{
    [XmlIgnore]
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }

    [XmlAttribute]
    public int X
    {
        get { return Location.X; }
        set { Location = new Point(value, Location.Y); }
    }

    [XmlAttribute]
    public int Y
    {
        get { return Location.Y; }
        set { Location = new Point(Location.X, value); }
    }
}
现在我明白了:

public class Tree
{
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }
}
AppleTree appleTree = new AppleTree
{
    Location = new Point(10, 20),
    FruitCount = 69
};
// Serialize ...
<?xml version="1.0"?>
<AppleTree FruitCount="69">
  <Location>
    <X>10</X>
    <Y>20</Y>
  </Location>
</AppleTree>
<?xml version="1.0"?>
<AppleTree FruitCount="69" X="10" Y="20" />
public class Tree
{
    [XmlIgnore]
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }

    [XmlAttribute]
    public int X
    {
        get { return Location.X; }
        set { Location = new Point(value, Location.Y); }
    }

    [XmlAttribute]
    public int Y
    {
        get { return Location.Y; }
        set { Location = new Point(Location.X, value); }
    }
}
我知道,我可以这样说:

public class Tree
{
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }
}
AppleTree appleTree = new AppleTree
{
    Location = new Point(10, 20),
    FruitCount = 69
};
// Serialize ...
<?xml version="1.0"?>
<AppleTree FruitCount="69">
  <Location>
    <X>10</X>
    <Y>20</Y>
  </Location>
</AppleTree>
<?xml version="1.0"?>
<AppleTree FruitCount="69" X="10" Y="20" />
public class Tree
{
    [XmlIgnore]
    public Point Location { get; set; }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }

    [XmlAttribute]
    public int X
    {
        get { return Location.X; }
        set { Location = new Point(value, Location.Y); }
    }

    [XmlAttribute]
    public int Y
    {
        get { return Location.Y; }
        set { Location = new Point(Location.X, value); }
    }
}
但我不希望所有属性都重复(这只是一个简单的示例)


那么还有别的解决办法吗?如果使用
XmlSerializer

的属性,将X和Y作为属性带到基类中,则可以获得所需的行为:

public class Tree
{

    [XmlIgnore]
    public Point Location { get; set;}

    [XmlAttribute]
    public double X { 
        get { return Location.X;} 
        set { Location = new Point(value, Location.Y); }
    }

    [XmlAttribute]
    public double Y { 
        get { return Location.Y;} 
        set { Location = new Point(Location.X, value); }
    }
}

public class AppleTree : Tree
{
    [XmlAttribute]
    public int FruitCount { get; set; }
}
为我序列化如下: