晚上好。我有一个像这样的XML结构:
string cRet = "<bands>";
cRet += "<artist>";
cRet += "<name>";
cRet += "Vasco";
cRet += "</name>";
cRet += "<lp>";
cRet += "<namelp>";
cRet += "Bollicine";
cRet += "</namelp>";
cRet += "<songs>";
cRet += "<song>";
cRet += "Bollicine";
cRet += "</song>";
cRet += "<song>";
cRet += "Vita Spericolata";
cRet += "</song>";
cRet += "<song>";
cRet += "Giocala";
cRet += "</song>";
cRet += "</songs>";
cRet += "</lp>";
cRet += "<lp>";
cRet += "<namelp>";
cRet += "Gli SPari Sopra";
cRet += "</namelp>";
cRet += "<songs>";
cRet += "<song>";
cRet += "Gli SPari Sopra";
cRet += "</song>";
cRet += "<song>";
cRet += "Gabri";
cRet += "</song>";
cRet += "<song>";
cRet += "Lo Show";
cRet += "</song>";
cRet += "</songs>";
cRet += "</lp>";
cRet += "</artist>";
cRet += "</bands>";
var serializer = new XmlSerializer(typeof(Root));
var root = (Root)serializer.Deserialize(new StringReader(cRet));
string cJson = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.Indented);
我定义:
[XmlRoot("bands"), JsonObject]
public class Root
{
[XmlElement, JsonProperty]
public string name { get; set; }
[XmlElement, JsonProperty]
public string albumName { get; set; }
[XmlElement, JsonProperty]
public string song { get; set; }
}
我获得:
{ "name": null, "albumName": null, "song": null }
如果我只使用一个级别,我会得到完美的结果。我如何定义";艺术家;以及";专辑";?
您有Visual Studio 2017/2019吗?如果是这样,您可以在Visual Studio中从JSON或XML生成类。一旦您正确定义了类,就应该能够使用Newtonsoft.Json来创建Json。
要从XML创建类,请执行以下操作:
创建示例XML:
注意:如果XML中的任何元素都有多个元素,请确保示例数据包含多个元素。
<bands>
<artist>
<name>Vasco</name>
<lp>
<namelp>Bollicine</namelp>
<songs>
<song>Bollicine</song>
<song>Vita Spericolata</song>
<song>Giocala</song>
</songs>
</lp>
</artist>
<artist>
<name>Other Artist</name>
<lp>
<namelp>Other Artist album name</namelp>
<songs>
<song>Song 1</song>
<song>Song 2</song>
</songs>
</lp>
</artist>
</bands>
Open Visual Studio 2017(或2019(
创建新项目(文件=>新项目=>(
添加using语句:使用System.Xml.Serialization;
创建新类(项目=>添加类…(名称:波段
在记事本中打开示例XML。
突出显示XML,右键单击,然后选择复制
在VS菜单中,单击编辑
选择粘贴特殊
选择将XML粘贴为类
您将获得以下信息:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class bands
{
private bandsArtist[] artistField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("artist")]
public bandsArtist[] artist
{
get
{
return this.artistField;
}
set
{
this.artistField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bandsArtist
{
private string nameField;
private bandsArtistLP lpField;
/// <remarks/>
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public bandsArtistLP lp
{
get
{
return this.lpField;
}
set
{
this.lpField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bandsArtistLP
{
private string namelpField;
private string[] songsField;
/// <remarks/>
public string namelp
{
get
{
return this.namelpField;
}
set
{
this.namelpField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("song", IsNullable = false)]
public string[] songs
{
get
{
return this.songsField;
}
set
{
this.songsField = value;
}
}
}
我对代码进行了一点修改/重构,最终得到了下面的代码。我改了类名的大小写。此外,为了按所需顺序排列相同深度的元素,我为一些类添加了接口(如.NET Serialization Ordering中所述(
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "bands")]
public partial class Bands
{
[System.Xml.Serialization.XmlElementAttribute("artist")]
public List<BandsArtist> artist { get; set; } = new List<BandsArtist>();
}
public interface IBandsArtist
{
string name { get; set; }
List<BandsArtistLP> lp { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BandsArtist : IBandsArtist
{
[XmlElement("name")]
public string name { get; set; }
[System.Xml.Serialization.XmlElementAttribute("lp")]
public List<BandsArtistLP> lp { get; set; } = new List<BandsArtistLP>();
}
public interface IBandsArtistLP
{
string namelp { get; set; }
List<string> songs { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BandsArtistLP : IBandsArtistLP
{
[XmlElement("namelp")]
public string namelp { get; set; }
[XmlArrayItemAttribute("song", IsNullable = false)]
public List<string> songs { get; set; } = new List<string>();
}
使用Newtonsoft.json将";"带";类(其中"myBands"是包含您的数据的类"bands"的实例(
string cJson = JsonConvert.SerializeObject(myBands, Newtonsoft.Json.Formatting.Indented);
生成的json是:
{
"artist": [
{
"name": "Vasco",
"lp": [
{
"namelp": "Bollicine",
"songs": [
"Bollicine",
"Vita Spericolata",
"Giocala"
]
}
]
},
{
"name": "Other Artist",
"lp": [
{
"namelp": "Other Artist album name",
"songs": [
"Song 1",
"Song 2"
]
}
]
}
]
}