首页 / SQL Server / 正文

[SQL Server]SQL Server数据库中如何使用SQL语句查询出指定数据库的所有索引?

1630 1 发布于: 2019-07-02 读完约需10分钟

问题描述

C#/.NET应用程序编程开发中,如何将JSON格式数据转换成XML格式,反之又如何将XML格式转换成JSON格式呢?

方案一

使用第三方组件库Json.NETJson.NET内置了JSON与XML两种数据格式的相互转换,转换示例如下:

XML转换成JSON

string xml = @"<?xml version='1.0' standalone='no'?>
<root>
  <person id='1'>
    <name>Alan</name>
    <url>http://www.google.com</url>
  </person>
  <person id='2'>
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
  </person>
</root>";

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

string jsonText = JsonConvert.SerializeXmlNode(doc);

输出结果:

{
  "?xml": {
    "@version": "1.0",
    "@standalone": "no"
  },
  "root": {
    "person": [
      {
        "@id": "1",
        "name": "Alan",
        "url": "http://www.google.com"
      },
      {
        "@id": "2",
        "name": "Louis",
        "url": "http://www.yahoo.com"
      }
    ]
  }
}

由于相同级别上具有相同名称的多个节点被分组到一个数组中,因此转换过程可以根据节点的数量生成不同的JSON。例如,如果某个用户的某个XML只有一个<Role>节点,那么默认情况下,该角色将被转换成对应的JSON文本,但是如果用户有多个<Role>节点,那么该角色值将被放置在JSON数组中,如:

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);

输出结果:

{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": "Admin1"
  }
}

在只有一个<Role>节点时,如果要将此节点转换成数组,则可以为节点添加数组属性来强制转换,如下:

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);
{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": [
      "Admin"
    ]
  }
}

这时,<Role>节点转换后就变成JSON中的数组了。

JSON转换成XML

string json = @"{
  '?xml': {
    '@version': '1.0',
    '@standalone': 'no'
  },
  'root': {
    'person': [
      {
        '@id': '1',
        'name': 'Alan',
        'url': 'http://www.google.com'
      },
      {
        '@id': '2',
        'name': 'Louis',
        'url': 'http://www.yahoo.com'
      }
    ]
  }
}";

XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

输出结果:

<?xml version="1.0" standalone="no"?>
<root>
  <person id="1">
    <name>Alan</name>
    <url>http://www.google.com</url>
  </person>
  <person id="2">
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
  </person>
</root>

方案二

使用.NET Framework内置的JSON序列化类库。

JSON转换成XML使用System.Runtime.Serializtion.Json,如下:

var xml = XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(
    Encoding.ASCII.GetBytes(jsonString), new XmlDictionaryReaderQuotas()));

XML转换成JSON使用System.Web.Script.Serialization,如下:

var json = new JavaScriptSerializer().Serialize(GetXmlData(XElement.Parse(xmlString)));

private static Dictionary<string, object> GetXmlData(XElement xml)
{
    var attr = xml.Attributes().ToDictionary(d => d.Name.LocalName, d => (object)d.Value);
    if (xml.HasElements) attr.Add("_value", xml.Elements().Select(e => GetXmlData(e)));
    else if (!xml.IsEmpty) attr.Add("_value", xml.Value);

    return new Dictionary<string, object> { { xml.Name.LocalName, attr } };
}

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

上一篇: C#/.NET应用程序编程开发中如何将JSON格式数据转换成XML格式或者将XML格式转换成JSON格式呢?

下一篇: [SQL Server]SQL Server数据库中如何修改数据列的数据类型?

本文永久链接码友网 » [SQL Server]SQL Server数据库中如何使用SQL语句查询出指定数据库的所有索引?

分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册