gpt4 book ai didi

c# - 将根 XML 元素反序列化为数组

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:46 26 4
gpt4 key购买 nike

我需要将 XML 中的多个字段保存到不同的数组中。这是我的 XML 的样子:

<Content>
<Colours>
<Colour name="Strong Red">
<R>255</R>
<G>0</G>
<B>0</B>
<A>255</A>
</Colour>
</Colours>
<Textures>
<Texture name="Character01">
<Path>Path/Folder</Path>
</Texture>
</Textures>
</Content>

现在当 <Colours> 时一切正常是我的根,我只将颜色添加到一个数组中。

现在我想通过同一个 XML 文件添加纹理和更多纹理,从而将根文件移动到 <Content>。 .

这就是我的 ColourLoader 类看起来只有颜色和 <Colour> 的样子作为我的 XML 的根:

[Serializable()]
[XmlRoot("Colours")]
public class ColourLoader
{
[XmlElement("Colour")]
public CustomColour[] Colours;

public static ColourLoader Load(string path)
{
var serializer = new XmlSerializer(typeof(ColourLoader));
using (var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as ColourLoader;
}
}
}

我的 CustomColour 类工作正常,它使用 [XmlElement("R")]等从 XML 中读取值。只是我不知道如何从嵌套的 XML 中读取元素。我不知何故必须跳过 <Content>并从 <Colours> 添加颜色以 root 身份对 <Textures> 执行相同操作等

我不想创建多个 XML 文件,因为我希望将所有内容管理到一个位置并且只加载一个 XML 文件一次。

最佳答案

我认为这就是您所追求的。我还包含了一个序列化对象的方法。我发现这对于诊断 XML 序列化的特殊问题非常有帮助...

如果这解决了您的问题,请投票作为答案。

    [Serializable()]
[XmlRoot("Content")]
public class Content
{
[XmlArray("Colours")]
[XmlArrayItem("Colour")]
public CustomColour[] Colours { get; set; }

[XmlArray("Textures")]
[XmlArrayItem("Texture")]
public CustomTexture[] Textures { get; set; }
}

[Serializable()]
[XmlRoot("Colour")]
public class CustomColour
{
[XmlAttribute("name")]
public string Name { get; set; }

[XmlElement("R")]
public int R { get; set; }

[XmlElement("G")]
public int G { get; set; }

[XmlElement("B")]
public int B { get; set; }

[XmlElement("A")]
public int A { get; set; }
}

[Serializable()]
[XmlRoot("Texture")]
public class CustomTexture
{
[XmlAttribute("name")]
public string Name { get; set; }

[XmlElement("Path")]
public string Path { get; set; }
}

public static class ContentLoader
{
public static Content Load(TextReader textReader)
{
var serializer = new XmlSerializer(typeof(Content));
var ret = serializer.Deserialize(textReader) as Content;
return ret;
}

public static void Save(TextWriter textWriter, Content content)
{
var serializer = new XmlSerializer(typeof(Content));
serializer.Serialize(textWriter, content);
}
}

public static void XmlSerializing()
{
var xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Content xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Colours>
<Colour name=""Strong Red"">
<R>255</R>
<G>0</G>
<B>0</B>
<A>255</A>
</Colour>
</Colours>
<Textures>
<Texture name=""Character01"">
<Path>Path/Folder</Path>
</Texture>
</Textures>
</Content>";
var reader = new StringReader(xml);
var content = ContentLoader.Load(reader);

Console.WriteLine("Deserialized version:");
Console.WriteLine(" Colours");
foreach (var colour in content.Colours)
{
Console.WriteLine(" R: {0}, G: {1}, B: {2}, A: {3}", colour.R, colour.G, colour.B, colour.A);
}

Console.WriteLine(" Textures");
foreach (var texture in content.Textures)
{
Console.WriteLine(" Path: {0}", texture.Path);
}

var contentObj = new Content()
{
Colours = new[] { new CustomColour() { Name = "StrongRed", R = 255, G = 0, B = 0, A = 255 } },
Textures = new[] { new CustomTexture() { Name = "Character01", Path = "Path/Folder" } }
};

Console.WriteLine(string.Empty);
Console.WriteLine("Serialized version:");
var writer = new StringWriter();
ContentLoader.Save(writer, contentObj);
Console.WriteLine(writer);
}

关于c# - 将根 XML 元素反序列化为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com