如何在C#中将* .RESX XML文件转换为JSON文件?
我有常规的RESX文件。例如:
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data xml:space="preserve" name="KEY_1">
<value>Text A</value>
</data>
<data xml:space="preserve" name="KEY_2">
<value>Text B</value>
</data>
</root>
我需要将此文件转换为JSON文件:
{
"Texts": [
{
"id": "KEY_1",
"text": "Text A"
},
{
"id": "KEY_2",
"text": "Text B"
}
]
}
正如您所看到的转换仅与数据相关:
<data xml:space="preserve" name="KEY_1">
<value>Text A</value>
</data>
<data xml:space="preserve" name="KEY_2">
<value>Text B</value>
</data>
其他一切与转型无关。
编辑: 我有解决方案,但我认为可以做得更好
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace XMLtoJSON
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> result = new Dictionary<string, string>();
// To convert an XML node contained in string xml into a JSON string
var xml = File.ReadAllText(@"C:\Test\ClientLocalization.en-US.resx");
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList node = doc.GetElementsByTagName("data");
foreach (XmlNode item in node)
{
var value = item.InnerText.Trim();
var keyName = item.Attributes.Cast<XmlAttribute>().FirstOrDefault(f => f.Name == "name");
if (keyName != null)
{
var key = keyName.InnerText.Trim();
result.Add(key, value);
}
}
string res = @"{ ""Text"" : [ ";
foreach (var item in result)
{
res += "{";
res += $" \"id\":\"{item.Key}\",\"text\":\"{item.Value}\"";
res += "},";
}
res = res.Remove(res.Length - 1);
res += @" ]} ";
}
}
}
答案 0 :(得分:4)
我不会使用import React from 'react'
const Counter = ({ value }) => {
return (
<h1>{value}</h1>
)
}
export default Counter
,而是使用official document ,因为它更符合LINQ。使用XmlDocument
加载XML,然后选择您感兴趣的节点并将它们整形为一个反映所需JSON的匿名对象结构。最后,使用您最喜欢的JSON序列化程序(例如XElement
或Json.Net)从那里创建JSON。我不建议手动滚动你自己的JSON,因为它非常容易出错。
XElement.Parse
答案 1 :(得分:2)