XmlElement的C#数组正在添加另一层嵌套
我有以下情况:XmlElement的C#数组正在添加另一层嵌套,c#,.net,xml,serialization,xml-serialization,C#,.net,Xml,Serialization,Xml Serialization,我有以下情况: 4个返回XML的方法 必须用数组中的这4个XML片段创建一个大XML 这方面的一个小例子如下: <garage> <owner>daniel</owner> <cars> <XmlElement> <plate>ABC123</plate> </XmlElement> <XmlElemen
- 4个返回XML的方法
- 必须用数组中的这4个XML片段创建一个大XML
<garage>
<owner>daniel</owner>
<cars>
<XmlElement>
<plate>ABC123</plate>
</XmlElement>
<XmlElement>
<plate>DSC563</plate>
</XmlElement>
<XmlElement>
<plate>AIO789</plate>
</XmlElement>
<XmlElement>
<plate>IUE692</plate>
</XmlElement>
</cars>
</garage>
我尝试过使用Xml属性,但无法得到我想要的东西
谁能帮我一下吗
以下是工作代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Xunit;
using Xunit.Abstractions;
namespace TestsProjec.XML
{
public class Tests
{
private readonly ITestOutputHelper _testOutputHelper;
public Tests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void InjectingXml()
{
var serializer = new XmlSerializer(typeof(Garage));
var cars = new List<XmlElement>();
cars.Add(StringToXmlElement("<plate>ABC123</plate>"));
cars.Add(StringToXmlElement("<plate>DSC563</plate>"));
cars.Add(StringToXmlElement("<plate>AIO789</plate>"));
cars.Add(StringToXmlElement("<plate>IUE692</plate>"));
string fullXml;
var entity = new Garage()
{
owner = "Daniel",
cars = cars.ToArray()
};
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter tw = XmlWriter.Create(ms))
{
serializer.Serialize(tw, entity);
try
{
byte[] tmp = new byte[ms.Length - 3];
ms.Position = 3; //to skip the UTF-8 preamble
ms.Read(tmp, 0, (int)ms.Length - 3);
fullXml = Encoding.UTF8.GetString(tmp);
}
catch
{
fullXml = null;
}
}
}
_testOutputHelper.WriteLine(fullXml);
}
public XmlElement StringToXmlElement(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
}
public class Garage
{
public string owner { get; set; }
public System.Xml.XmlElement[] cars { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用Xunit;
使用Xunit.抽象;
命名空间TestsProjec.XML
{
公开课考试
{
私有只读ITestOutputHelper\u testOutputHelper;
公共测试(ITestOutputHelper testOutputHelper)
{
_testOutputHelper=testOutputHelper;
}
[事实]
公共void InjectingXml()
{
var serializer=新的XmlSerializer(typeof(Garage));
var cars=新列表();
添加(StringToxmlement(“ABC123”);
添加(StringToXmlElement(“DSC563”);
添加(StringToxmlement(“AIO789”);
添加(StringToxmlement(“IUE692”);
字符串fullXml;
var entity=newgarage()
{
所有者=“丹尼尔”,
cars=cars.ToArray()
};
使用(MemoryStream ms=new MemoryStream())
{
使用(XmlWriter tw=XmlWriter.Create(ms))
{
serializer.Serialize(tw,entity);
尝试
{
字节[]tmp=新字节[ms.Length-3];
ms.Position=3;//跳过UTF-8前导
ms.Read(tmp,0,(int)ms.Length-3);
fullXml=Encoding.UTF8.GetString(tmp);
}
抓住
{
fullXml=null;
}
}
}
_testOutputHelper.WriteLine(fullXml);
}
公共XmlElement StringToXmlElement(字符串xml)
{
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xml);
返回doc.document元素;
}
}
公共级车库
{
公共字符串所有者{get;set;}
public System.Xml.xmlement[]cars{get;set;}
}
}
一个选项是使用XmlSerializer并拥有表示xml的类
class Program
{
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Garage));
var garage = new Garage
{
Owner = new Owner { Name = "Daniel" },
Cars = new List<string>
{
"ABC123",
"DSC563",
"AIO789",
"IUE692",
}
};
serializer.Serialize(File.Create("file.xml"), garage);
}
}
[Serializable]
public class Garage
{
[XmlElement("Owner")]
public Owner Owner;
[XmlArrayItem("Plate")]
public List<string> Cars;
}
[Serializable]
public class Owner
{
[XmlElement("Name")]
public string Name;
}
类程序
{
静态void Main(字符串[]参数)
{
XmlSerializer serializer=新的XmlSerializer(typeof(Garage));
var车库=新车库
{
所有者=新所有者{Name=“Daniel”},
汽车=新名单
{
“ABC123”,
“DSC563”,
“AIO789”,
“IUE692”,
}
};
serializer.Serialize(File.Create(“File.xml”),garage);
}
}
[可序列化]
公共级车库
{
[XmlElement(“所有者”)]
公共所有者;
[XmlArrayItem(“板”)]
公共车辆清单;
}
[可序列化]
公共类所有者
{
[XmlElement(“名称”)]
公共字符串名称;
}
如果使用XElement
,而不是XmlElement
,则可以使用XmlAnyElement
属性获得所需的输出:
public class Garage
{
public string owner { get; set; }
[XmlAnyElement]
public XElement[] cars { get; set; }
}
您的方法stringToxmlement
将如下所示:
public XElement StringToXmlElement(string xml)
{
return XElement.Parse(xml);
}
编写自定义序列化程序或将
Garage
更改为一个具有1:1属性的xml类(您可以使用在线工具)您可以这样做,但这意味着展开每个XmlElement
,并复制有关板的知识
名称:也许您应该创建一个类Car
。作为一个选择,谢谢你的想法,但是正如我所说的,这只是真实情况的一个例子(这更复杂)。
public XElement StringToXmlElement(string xml)
{
return XElement.Parse(xml);
}