这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<Fruit>
<Fruit_group name='Tropical'>
<fruit_types name ='Tropical Used'>
<fruit>bananas</fruit>
<fruit>mangoes</fruit>
</fruit_types>
</Fruit_group>
<Fruit_group name='Citrus'>
<fruit_types name ='Citruses Used'>
<fruit>orange</fruit>
<fruit>lime</fruit>
<fruit>grapefruit</fruit>
<excluded_fruits>
<fruit>mandarin</fruit>
</excluded_fruits>
</fruit_types>
</Fruit_group>
</Fruit>
这是我的XML文本的示例。.
是否有一种方法可以反序列化它并保留元素的名称。
我的意思是我想拥有:
Tropical -> Tropical Used -> fruit -> bananas, mangoes
Citrus->Citruses Used ->
fruit = orange, lime, grapefruit
excluded = mandarin....
Something like this...
有人可以帮助我了解其工作原理吗?
答案 0 :(得分:0)
您可以使用XML to c#网站根据XML创建类。
在这种情况下,我有这个例子
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="fruit_types")]
public class Fruit_types {
[XmlElement(ElementName="fruit")]
public List<string> Fruit { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlElement(ElementName="excluded_fruits")]
public Excluded_fruits Excluded_fruits { get; set; }
}
[XmlRoot(ElementName="Fruit_group")]
public class Fruit_group {
[XmlElement(ElementName="fruit_types")]
public Fruit_types Fruit_types { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="excluded_fruits")]
public class Excluded_fruits {
[XmlElement(ElementName="fruit")]
public string Fruit { get; set; }
}
[XmlRoot(ElementName="Fruit")]
public class Fruit {
[XmlElement(ElementName="Fruit_group")]
public List<Fruit_group> Fruit_group { get; set; }
}
}
此外,您可以阅读有关XML反序列化的article。
答案 1 :(得分:0)
我建议使用xmltocsharp将XML
转换为Model
并进行必要的更改。例如,将string
下的List<string>
更改为Excluded_fruits
[XmlRoot(ElementName = "fruit_types")]
public class Fruit_types
{
[XmlElement(ElementName = "fruit")]
public List<string> Fruit { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "excluded_fruits")]
public Excluded_fruits Excluded_fruits { get; set; }
}
[XmlRoot(ElementName = "Fruit_group")]
public class Fruit_group
{
[XmlElement(ElementName = "fruit_types")]
public Fruit_types Fruit_types { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "excluded_fruits")]
public class Excluded_fruits
{
[XmlElement(ElementName = "fruit")]
public List<string> Fruit { get; set; }
}
[XmlRoot(ElementName = "Fruit")]
public class Fruit
{
[XmlElement(ElementName = "Fruit_group")]
public List<Fruit_group> Fruit_group { get; set; }
}
模型准备就绪后,您可以使用XmlSerializer
来Deserialize
xml文件
using (StreamReader r = new StreamReader(xmlfilepath))
{
string xmlString = r.ReadToEnd();
XmlSerializer ser = new XmlSerializer(typeof(Fruit));
using (TextReader reader = new StringReader(xmlString))
{
var fruits = (Fruit)ser.Deserialize(reader);
foreach(var fruitgroup in fruits.Fruit_group)
{
Console.Write($"{fruitgroup.Name} -> ");
Console.Write($"{fruitgroup.Fruit_types.Name} -> ");
Console.Write($"fruit = {string.Join(",", fruitgroup.Fruit_types.Fruit.ToArray())}; ");
if (fruitgroup.Fruit_types.Excluded_fruits?.Fruit?.Count() > 0)
Console.Write($"excluded = {string.Join(",", fruitgroup.Fruit_types.Excluded_fruits.Fruit.ToArray())}{Environment.NewLine}");
else
Console.WriteLine();
}
}
}
答案 2 :(得分:0)
我喜欢使用xml linq进行自定义解析以获得更平坦的结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<Fruit_Group> groups = doc.Descendants("Fruit_group").Select(x => new Fruit_Group()
{
groupName = (string)x.Attribute("name"),
typeName = (string)x.Element("fruit_types").Attribute("name"),
fruits = x.Elements("fruit_types").Elements("fruit").Select(y => (string)y).ToArray(),
excluded = x.Descendants("excluded_fruits").Elements("fruit").Select(y => (string)y).ToArray()
}).ToList();
}
}
public class Fruit_Group
{
public string groupName { get; set;}
public string typeName { get; set; }
public string[] fruits { get; set; }
public string[] excluded { get; set; }
}
}