【问题标题】:Convert xml to json ignoring element prefix将xml转换为json忽略元素前缀
【发布时间】:2019-01-17 23:38:34
【问题描述】:

如何在忽略每个元素上的前缀“zs:”的情况下将 xml 转换为 json。

<?xml version="1.0" encoding="UTF-8" ?>
 <zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/">
   <zs:version>1.1</zs:version>
   <zs:numberOfRecords>0</zs:numberOfRecords>
   <zs:echoedSearchRetrieveRequest>
       <zs:version>1.1</zs:version>
       <zs:query>dc.identifier="9780393051247"</zs:query>
       <zs:startRecord>1</zs:startRecord>
       <zs:maximumRecords>10</zs:maximumRecords>
       <zs:recordPacking>xml</zs:recordPacking>
       <zs:recordSchema>marcxml</zs:recordSchema>
   </zs:echoedSearchRetrieveRequest>

【问题讨论】:

    标签: c# .net json xml


    【解决方案1】:

    您可以使用自定义的 JsonTextWriter。

    public class CustomJsonWriter : JsonTextWriter
    {
        public CustomJsonWriter(TextWriter writer): base(writer){}
    
        public override void WritePropertyName(string name)
        {
            base.WritePropertyName(name.Replace("zs:",string.Empty));
        }
    }
    

    然后

    var xDocument = XDocument.Parse(xml);
    var builder = new StringBuilder();
    using(var writer = new StringWriter(builder))
    {
        var serializer = JsonSerializer.Create();
        serializer.Serialize(new CustomJsonWriter(writer), xDocument);
    }
    

    输出

    {
      "?xml": {
        "@version": "1.0",
        "@encoding": "UTF-8"
      },
      "searchRetrieveResponse": {
        "@xmlns:zs": "http://www.loc.gov/zing/srw/",
        "version": "1.1",
        "numberOfRecords": "0",
        "echoedSearchRetrieveRequest": {
          "version": "1.1",
          "query": "dc.identifier='9780393051247'",
          "startRecord": "1",
          "maximumRecords": "10",
          "recordPacking": "xml",
          "recordSchema": "marcxml"
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      Json.Net 有一个库。我在LinqPad.Net 中玩过你的xml。

      这段代码:

      var xml = new StringBuilder();
      
      xml.AppendLine("<?xml version = \"1.0\" encoding = \"UTF-8\"?>");
      xml.AppendLine("<zs:searchRetrieveResponse xmlns:zs = \"http://www.loc.gov/zing/srw/\">");
      xml.AppendLine("<zs:version>1.1</zs:version >");
      xml.AppendLine("<zs:numberOfRecords>0</zs:numberOfRecords>");
      xml.AppendLine("<zs:echoedSearchRetrieveRequest>");
      xml.AppendLine("<zs:version>1.1</zs:version>");
      xml.AppendLine("<zs:query>dc.identifier=\"9780393051247\"</zs:query>");
      xml.AppendLine("<zs:startRecord>1</zs:startRecord>");
      xml.AppendLine("<zs:maximumRecords>10</zs:maximumRecords>");
      xml.AppendLine("<zs:recordPacking>xml</zs:recordPacking>");
      xml.AppendLine("<zs:recordSchema>marcxml</zs:recordSchema>");
      xml.AppendLine("</zs:echoedSearchRetrieveRequest>");
      xml.AppendLine("</zs:searchRetrieveResponse>");
      
      xml.Replace("zs:", string.Empty);
      xml.Replace(":zs", string.Empty);
      
      
      var xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(xml.ToString());
      
      var json = JsonConvert.SerializeXmlNode(xmlDoc);
      
      json.Dump();
      

      产生这个结果:

      {
         "?xml":{
            "@version":"1.0",
            "@encoding":"UTF-8"
         },
         "searchRetrieveResponse":{
            "@xmlns":"http://www.loc.gov/zing/srw/",
            "version":"1.1",
            "numberOfRecords":"0",
            "echoedSearchRetrieveRequest":{
               "version":"1.1",
               "query":"dc.identifier=\"9780393051247\"",
               "startRecord":"1",
               "maximumRecords":"10",
               "recordPacking":"xml",
               "recordSchema":"marcxml"
            }
         }
      }
      

      【讨论】:

        【解决方案3】:

        您不能只忽略命名空间 - 它是不可或缺的元素。请使用:

        string xmlWithoutNamespace = yourXmlString.Replace("zs:", "");
        

        在您的 xml 字符串上,然后将其转换为 json。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-19
          • 1970-01-01
          • 2021-06-20
          • 1970-01-01
          • 1970-01-01
          • 2021-10-30
          相关资源
          最近更新 更多