string xml = @"<person id='1'>
  <name>Alan</name>
  <url>http://www.google.com</url>
  <role>Admin1</role>
</person>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "person": {
//     "@id": "1",
//     "name": "Alan",
//     "url": "http://www.google.com",
//     "role": "Admin1"
//   }
// }

xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
  <name>Alan</name>
  <url>http://www.google.com</url>
  <role json:Array='true'>Admin</role>
</person>";

doc = new XmlDocument();
doc.LoadXml(xml);

json = JsonConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "person": {
//     "@id": "1",
//     "name": "Alan",
//     "url": "http://www.google.com",
//     "role": [
//       "Admin"
//     ]
//   }
// }

 

相关文章:

  • 2021-12-27
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-10-26
  • 2022-01-02
相关资源
相似解决方案