如何使用 XmlSerializer 动态序列化 XML
How to serialise XML dynamically with XmlSerializer
这是我的 XML 结构:
<codelang1>
<prox>prox dans la langue</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
<prox>prox dans la langue</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang2>
...
<codelang...n>
<libelle></libelle>
....
</codelang...n>
如何使用 class c# 序列化此 XML?代码语言发生变化。
有点晚了但是试试这个.....
使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
类
public class Codelang
{
public string prox { get; set; }
public string libelle { get; set; }
public string descriptif { get; set; }
}
代码
class Program
{
static void Main(string[] args)
{
string xmlfilepath = @"xml.xml";
XDocument xmlsrcdoc = XDocument.Load(xmlfilepath);
List<Codelang> lstCodelang = new List<Codelang>();
try
{
lstCodelang = xmlsrcdoc.Descendants()
.Elements("prox")
.Select(el => FromXElement<Codelang>(el.Parent))
.ToList();
}
catch (Exception)
{
throw;
}
}
public static T FromXElement<T>(XElement element) where T : class, new()
{
var typeOfT = typeof(T);
T value = new T();
foreach (var subElement in element.Elements())
{
var prop = typeOfT.GetProperty(subElement.Name.LocalName);
if (prop != null)
{
prop.SetValue(value, subElement.Value);
}
}
return value;
}
}
XML
<root>
<codelang1>
<prox>prox dans la langue 1</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
<prox>prox dans la langue 2</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang2>
<codelang3>
<prox>prox dans la langue 3</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang3>
</root>
我正在从名为 xml.xml 的应用程序构建文件夹中的一个文件中将您的 XML 读入一个字符串...您需要从其他地方获取 XML 字符串或者创建 xml.xml 文件并保存你的 XML 以使上面的代码工作
这是我的 XML 结构:
<codelang1>
<prox>prox dans la langue</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
<prox>prox dans la langue</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang2>
...
<codelang...n>
<libelle></libelle>
....
</codelang...n>
如何使用 class c# 序列化此 XML?代码语言发生变化。
有点晚了但是试试这个.....
使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
类
public class Codelang
{
public string prox { get; set; }
public string libelle { get; set; }
public string descriptif { get; set; }
}
代码
class Program
{
static void Main(string[] args)
{
string xmlfilepath = @"xml.xml";
XDocument xmlsrcdoc = XDocument.Load(xmlfilepath);
List<Codelang> lstCodelang = new List<Codelang>();
try
{
lstCodelang = xmlsrcdoc.Descendants()
.Elements("prox")
.Select(el => FromXElement<Codelang>(el.Parent))
.ToList();
}
catch (Exception)
{
throw;
}
}
public static T FromXElement<T>(XElement element) where T : class, new()
{
var typeOfT = typeof(T);
T value = new T();
foreach (var subElement in element.Elements())
{
var prop = typeOfT.GetProperty(subElement.Name.LocalName);
if (prop != null)
{
prop.SetValue(value, subElement.Value);
}
}
return value;
}
}
XML
<root>
<codelang1>
<prox>prox dans la langue 1</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
<prox>prox dans la langue 2</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang2>
<codelang3>
<prox>prox dans la langue 3</prox>
<libelle>libellé dans la langue</libelle>
<descriptif>descriptif dans la langue</descriptif>
</codelang3>
</root>
我正在从名为 xml.xml 的应用程序构建文件夹中的一个文件中将您的 XML 读入一个字符串...您需要从其他地方获取 XML 字符串或者创建 xml.xml 文件并保存你的 XML 以使上面的代码工作