我通过套接字接收XML字符串,并希望将这些转换为c#对象。
这些消息的形式是:
<msg>
<id>1</id>
<action>stop</action>
</msg>
如何做到这一点呢?
我通过套接字接收XML字符串,并希望将这些转换为c#对象。
这些消息的形式是:
<msg>
<id>1</id>
<action>stop</action>
</msg>
如何做到这一点呢?
当前回答
public string Serialize<T>(T settings)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringWriter outStream = new StringWriter();
serializer.Serialize(outStream, settings);
return outStream.ToString();
}
其他回答
我知道这个问题很老了,但我无意中发现了它,我有一个和其他人不同的答案:-)
通常的方法(如上面的评论者所述)是生成一个类并反序列化xml。
但是(警告:这里是无耻的自我推销)我刚刚发布了一个小包,在这里,你不必使用它。你只要说:
string xml = System.IO.File.ReadAllText(@"C:\test\books.xml");
var book = Dandraka.XmlUtilities.XmlSlurper.ParseText(xml);
这就是它的字面意思,没有其他需要。最重要的是,如果xml发生变化,对象也会自动发生变化。
如果您喜欢直接下载dll, github页面在这里。
另一种高级xsd到c#类生成工具:xsd2code.com。这个工具非常方便和强大。它比Visual Studio中的xsd.exe工具有更多的自定义。xsd2code++可以定制为使用列表或数组,并支持包含大量Import语句的大型模式。
注意一些特征,
Generates business objects from XSD Schema or XML file to flexible C# or Visual Basic code. Support Framework 2.0 to 4.x Support strong typed collection (List, ObservableCollection, MyCustomCollection). Support automatic properties. Generate XML read and write methods (serialization/deserialization). Databinding support (WPF, Xamarin). WCF (DataMember attribute). XML Encoding support (UTF-8/32, ASCII, Unicode, Custom). Camel case / Pascal Case support. restriction support ([StringLengthAttribute=true/false], [RegularExpressionAttribute=true/false], [RangeAttribute=true/false]). Support large and complex XSD file. Support of DotNet Core & standard
创建一个DTO作为CustomObject
使用下面的方法使用JAXB将XML字符串转换为DTO
private static CustomObject getCustomObject(final String ruleStr) {
CustomObject customObject = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CustomObject.class);
final StringReader reader = new StringReader(ruleStr);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
customObject = (CustomObject) jaxbUnmarshaller.unmarshal(reader);
} catch (JAXBException e) {
LOGGER.info("getCustomObject parse error: ", e);
}
return customObject;
}
在这个日期(2020-07-24),我已经看过了所有的答案,必须有一个更简单、更熟悉的方法来解决这个问题,这就是下面。
两个场景……一个是XML字符串是否格式良好,即它以<?XML版本="1.0"编码="utf-16"?>或类似的值,然后再遇到根元素,即问题中的<msg>。另一种是如果它不是良好形式的,即只有根元素(例如问题中的<msg>)和它的子节点。
首先,只是一个简单的类,其中包含以不区分大小写的名称匹配XML中根节点的子节点的属性。所以,从问题来看,它会是这样的…
public class TheModel
{
public int Id { get; set; }
public string Action { get; set; }
}
下面是剩下的代码…
// These are the key using statements to add.
using Newtonsoft.Json;
using System.Xml;
bool isWellFormed = false;
string xml = = @"
<msg>
<id>1</id>
<action>stop</action>
</msg>
";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
if (isWellFormed)
{
xmlDocument.RemoveChild(xmlDocument.FirstChild);
/* i.e. removing the first node, which is the declaration part.
Also, if there are other unwanted parts in the XML,
write another similar code to locate the nodes
and remove them to only leave the desired root node
(and its child nodes).*/
}
var serializedXmlNode = JsonConvert.SerializeXmlNode(
xmlDocument,
Newtonsoft.Json.Formatting.Indented,
true
);
var theDesiredObject = JsonConvert.DeserializeObject<TheModel>(serializedXmlNode);
有两种可能。
方法1。XSD的工具
假设XML文件位于C:\path\to\ XML \file. XML
打开开发人员命令提示符 你可以在开始菜单>程序> Microsoft Visual Studio 2012 > Visual Studio工具中找到它 或者如果你有Windows 8,可以在开始屏幕中输入开发人员命令提示符 输入cd /D "C:\path\to\ XML " 通过输入XSD file.xml从xml文件创建XSD文件 通过输入xsd / C file.xsd创建c#类
就是这样!您已经从C:\path\到\xml\file.cs中的xml文件生成了c#类
方法2 -粘贴特殊
要求Visual Studio 2012+与。net框架>= 4.5作为项目目标和“Windows通信基础”单独组件安装
将XML文件的内容复制到剪贴板 向您的解决方案添加新的空类文件(Shift+Alt+C) 打开该文件,在菜单中单击编辑>粘贴特殊>粘贴XML作为类
就是这样!
使用
这个helper类的用法非常简单:
using System;
using System.IO;
using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
using System.Xml;
using System.Xml.Serialization;
namespace Helpers
{
internal static class ParseHelpers
{
private static JavaScriptSerializer json;
private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } }
public static Stream ToStream(this string @this)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(@this);
writer.Flush();
stream.Position = 0;
return stream;
}
public static T ParseXML<T>(this string @this) where T : class
{
var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
}
public static T ParseJSON<T>(this string @this) where T : class
{
return JSON.Deserialize<T>(@this.Trim());
}
}
}
你现在要做的就是:
public class JSONRoot
{
public catalog catalog { get; set; }
}
// ...
string xml = File.ReadAllText(@"D:\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(@"D:\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();