2016-04-29 9 views
-1

私のコードをXMLにシリアル化したいと思います。今キー値のXMLシリアル化ノード名

私が持っている:

<?xml version="1.0" encoding="utf-8"?> 
<SerializationClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>Test</Value> 
</SerializationClass> 

と私のC#のコードは次のようになります。

​​

しかし、私はのようなXMLを持っているしたいと思います:

<?xml version="1.0" encoding="utf-8"?> 
<SerializationClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <City value="Test"></City> 
</SerializationClass> 
+0

1)http://stackoverflow.com/help/how-to-ask 2)必要なXMLが無効です。 (MyCityNameタグを参照してください) 3)何か試してみて、何を試しましたか? – bit

+0

XMLを取得するのが好きです:/? – Cieja

答えて

1

あなたが行うことはできませんそれをあなたが持っているクラス構造で(手作業でコーディングせずに)。構造を変更しても構わない場合、これはうまくいくはずです:

public class SerializationClass 
{ 
    public City City { get; set; } 
} 

public class City 
{ 
    [XmlAttribute("value")] 
    public string Value { get; set; } 
} 
0

このアプローチはどうですか?

public class SerializationClass 
{ 
    [XmlIgnore] 
    public string City { get; set; } 

    [EditorBrowsable(EditorBrowsableState.Never)] 
    [XmlAnyElement] 
    public XElement _City 
    { 
     get { return new XElement("City", new XAttribute("value", City)); } 
     set { City = value.FirstAttribute.Value; } 
    } 
}