如何在 C# 中将 *.RESX XML 文件转换为 JSON 文件
How to convert *.RESX XML file to JSON file in C#
如何在 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>
其他一切都与转换无关。
- Name in data attribute XML => is id in JSON file.
- Value in XML=> is text in JSON file.
编辑:
我有解决方案,但我认为可以做得更好
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 += @" ]} ";
}
}
}
我向您推荐 JSON.NET 框架。
它具有将 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"
// }
// ]
// }
//}
我不会使用 XmlDocument
,而是使用 XElement
as it is much more LINQ-friendly. Use XElement.Parse
to load the XML, then select the nodes you are interested in and shape them into an anonymous object structure that mirrors the JSON you want. Finally, use your favorite JSON serializer (such as Json.Net or JavaScriptSerializer
) 从那里创建 JSON。我不建议您自己手动滚动 JSON,因为它很容易出错。
var xml = File.ReadAllText(@"C:\Test\ClientLocalization.en-US.resx");
var obj = new
{
Texts = XElement.Parse(xml)
.Elements("data")
.Select(el => new
{
id = el.Attribute("name").Value,
text = el.Element("value").Value.Trim()
})
.ToList()
};
string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
// OR:
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string json = jss.Serialize(obj);
Console.WriteLine(json);
Fiddle: https://dotnetfiddle.net/ZIaCjd
如何在 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>
其他一切都与转换无关。
- Name in data attribute XML => is id in JSON file.
- Value in XML=> is text in JSON file.
编辑: 我有解决方案,但我认为可以做得更好
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 += @" ]} ";
}
}
}
我向您推荐 JSON.NET 框架。 它具有将 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"
// }
// ]
// }
//}
我不会使用 XmlDocument
,而是使用 XElement
as it is much more LINQ-friendly. Use XElement.Parse
to load the XML, then select the nodes you are interested in and shape them into an anonymous object structure that mirrors the JSON you want. Finally, use your favorite JSON serializer (such as Json.Net or JavaScriptSerializer
) 从那里创建 JSON。我不建议您自己手动滚动 JSON,因为它很容易出错。
var xml = File.ReadAllText(@"C:\Test\ClientLocalization.en-US.resx");
var obj = new
{
Texts = XElement.Parse(xml)
.Elements("data")
.Select(el => new
{
id = el.Attribute("name").Value,
text = el.Element("value").Value.Trim()
})
.ToList()
};
string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
// OR:
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string json = jss.Serialize(obj);
Console.WriteLine(json);
Fiddle: https://dotnetfiddle.net/ZIaCjd