XmlElement 的 C# 数组正在添加另一层嵌套
C# Array of XmlElement is adding another level of nesting
我有以下情况:
- 4 个方法 return 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>
我有一个 plate
数组,想将其注入到 car
中,这是一个 System.Xml.XmlElement[]
问题是,我似乎找不到摆脱包裹我的对象的 XmlElement
元素的方法,我只是希望它像:
<garage>
<owner>
<name>daniel</name>
</owner>
<cars>
<plate>ABC123</plate>
<plate>DSC563</plate>
<plate>AIO789</plate>
<plate>IUE692</plate>
</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; }
}
}
一个选项是使用 XmlSerializer 并让 class 代表您的 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;
}
如果你使用 XElement
而不是 XmlElement
,你可以得到你想要的输出,XmlAnyElement
属性:
public class Garage
{
public string owner { get; set; }
[XmlAnyElement]
public XElement[] cars { get; set; }
}
您的方法 StringToXmlElement
将如下所示:
public XElement StringToXmlElement(string xml)
{
return XElement.Parse(xml);
}
我有以下情况:
- 4 个方法 return 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>
我有一个 plate
数组,想将其注入到 car
中,这是一个 System.Xml.XmlElement[]
问题是,我似乎找不到摆脱包裹我的对象的 XmlElement
元素的方法,我只是希望它像:
<garage>
<owner>
<name>daniel</name>
</owner>
<cars>
<plate>ABC123</plate>
<plate>DSC563</plate>
<plate>AIO789</plate>
<plate>IUE692</plate>
</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; }
}
}
一个选项是使用 XmlSerializer 并让 class 代表您的 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;
}
如果你使用 XElement
而不是 XmlElement
,你可以得到你想要的输出,XmlAnyElement
属性:
public class Garage
{
public string owner { get; set; }
[XmlAnyElement]
public XElement[] cars { get; set; }
}
您的方法 StringToXmlElement
将如下所示:
public XElement StringToXmlElement(string xml)
{
return XElement.Parse(xml);
}