You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#对象序列化为YAML时出现缺失项及异常标识问题求助

解析配方文本并序列化为YAML时的问题排查

我尝试将以下格式的文本解析为Recipe对象后序列化为YAML:

m_list 2 1035 6 16 204 206 501
pdtse 1 6
m_list 3 1 1027 10 1035 1 2038 8 -1
pdtse 1 16
m_list 3 1 1027 8 1035 1 2042 6 -1
pdtse 1 204
m_list 3 1 1027 15 1035 1 2042 10 -1
pdtse 1 206
m_list 3 1 1027 8 1035 1 2046 7 -1
pdtse 1 501
m_list 3 1 500 1 1027 14 1035 1 -1
pdtclose

但序列化结果出现缺失项,还存在*o0、&o0这类异常标识。经过调研和调试仍无法定位问题原因,以下是我的处理代码及实体类定义,恳请帮忙排查:

处理代码

string[] lines = File.ReadAllLines(Path.Combine(_configuration.BasePath, _configuration.RecipeFile));
List<Recipe> recipes = new();
Recipe tempRecipe = new();
long vnumToInsert = 0;

foreach (string line in lines)
{
    string[] parts = line.Split(' ');

    if (line.StartsWith("m_list 2"))
    {
        foreach (string part in parts.Skip(3))
        {
            if (tempRecipe.Recipes == null)
            {
                tempRecipe.Recipes = new List<RecipeAttributes>()
                {
                    new RecipeAttributes
                    {
                        ItemVnum = Convert.ToInt64(part),
                        ProducerItemVnum = Convert.ToInt64(parts[2]),
                        Amount = 1
                    }
                };
            }
            else
            {
                tempRecipe.Recipes.Add(new RecipeAttributes
                {
                    ItemVnum = Convert.ToInt64(part),
                    ProducerItemVnum = Convert.ToInt64(parts[2]),
                    Amount = 1
                });
            }
        }
    }
    
    if (line.StartsWith("pdtse"))
    {
        vnumToInsert = Convert.ToInt64(parts[2]);
    }

    if (line.StartsWith("m_list 3"))
    {
        for (int i = 3; i < parts.Length; i += 2)
        {
            if (Convert.ToInt64(parts[i]) < 0) continue;

            if (tempRecipe.Recipes == null) continue;

            var findRecipeRelatedToVnum = tempRecipe.Recipes.FirstOrDefault(s => s.ItemVnum == vnumToInsert);

            if (findRecipeRelatedToVnum == null) break;

            if (findRecipeRelatedToVnum.Items == null)
            {
                findRecipeRelatedToVnum.Items = new List<ItemAttributes>()
                {
                    new ItemAttributes()
                    {
                        ItemVNum = Convert.ToInt64(parts[i]),
                        Amount = Convert.ToInt64(parts[i + 1])
                    }
                };
            }
            else
            {
                if (findRecipeRelatedToVnum.Items.FirstOrDefault(s => s.ItemVNum == Convert.ToInt64(parts[i])) != null) continue;

                findRecipeRelatedToVnum.Items.Add(new ItemAttributes()
                {
                    ItemVNum = Convert.ToInt64(parts[i]),
                    Amount = Convert.ToInt64(parts[i + 1])
                });
            }
        }
    }

    if (line.StartsWith("pdtclose"))
    {
        recipes.Add(tempRecipe);
        tempRecipe = new();
    }
}


Recipe toSerialize = new();
List<long> alreadyDone = new();
foreach (Recipe recipe in recipes)
{
    toSerialize = new();

    if (recipe.Recipes == null) continue;

    if (alreadyDone.Contains(recipe.Recipes[0].ProducerItemVnum)) continue;

    foreach (Recipe r in recipes.Where(s => s.Recipes?.First().ProducerItemVnum == recipe.Recipes?.First().ProducerItemVnum))
    {
        toSerialize = r;
    }

    alreadyDone.Add(recipe.Recipes[0].ProducerItemVnum);
    var serializer = new SerializerBuilder().Build();
    var yaml = serializer.Serialize(toSerialize);
    Console.WriteLine(yaml);
}

实体类定义

using YamlDotNet.Serialization;

namespace YamlParser.Entities
{
    public record Recipe
    {
        [YamlMember(Alias = "recipes")]
        public List<RecipeAttributes> Recipes { get; set; }
    }

    public record RecipeAttributes
    {
        [YamlMember(Alias = "item_vnum")]
        public long ItemVnum { get; set; }

        [YamlMember(Alias = "quantity")]
        public long Amount { get; set; }

        [YamlMember(Alias = "producer_item_vnum")]
        public long ProducerItemVnum { get; set; }

        [YamlMember(Alias = "items")]
        public List<ItemAttributes> Items { get; set; }
    }

    public record ItemAttributes
    {
        [YamlMember(Alias = "item_vnum")]
        public long ItemVNum { get; set; }

        [YamlMember(Alias = "quantity")]
        public long Amount { get; set; }
    }
}

感谢您的帮助!


内容的提问来源于stack exchange,提问作者Fizo55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:45:43