Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML转换为JSON,然后反序列化为对象_C#_Json_Xml_Json.net - Fatal编程技术网

C# 将XML转换为JSON,然后反序列化为对象

C# 将XML转换为JSON,然后反序列化为对象,c#,json,xml,json.net,C#,Json,Xml,Json.net,我正在用.NET Framework 4.7和JSON.NET 10.0.2开发一个ASP.NET MVC应用程序 我想加载一个xml文档,将其转换为JSON,然后反序列化为object。我想这样做是因为我想使用JSON文件和XML文件 这是我要使用该文件创建的类: public class ProductionOrderFile { public string ProductionOrderName { get; set; } public string ProductCode

我正在用.NET Framework 4.7和JSON.NET 10.0.2开发一个ASP.NET MVC应用程序

我想加载一个xml文档,将其转换为JSON,然后反序列化为object。我想这样做是因为我想使用JSON文件和XML文件

这是我要使用该文件创建的类:

public class ProductionOrderFile
{
    public string ProductionOrderName { get; set; }
    public string ProductCode { get; set; }
    public List<Batch> Batches { get; set; }
    public List<AggregationLevelConfiguration> Levels { get; set; }
    public List<VariableData> VariableData { get; set; }
}
但是这
返回JsonConvert.DeserializeObject(jsonText)
返回
ProductionOrderFile
的实例,其所有属性为空

可能问题在于XML文档没有正确的格式

XML文档的格式是否正确?
我做错了什么?

对XML进行反序列化的最简单方法是使用。为此,您可以将
列表
属性标记为,以指示它们应序列化为不带外部容器元素的重复元素集合:

public class ProductionOrderFile
{
    public string ProductionOrderName { get; set; }
    public string ProductCode { get; set; }
    [System.Xml.Serialization.XmlElement]
    public List<Batch> Batches { get; set; }
    [System.Xml.Serialization.XmlElement]
    public List<AggregationLevelConfiguration> Levels { get; set; }
    [System.Xml.Serialization.XmlElement]
    public List<VariableData> VariableData { get; set; }
}
样品

也就是说,如果您坚持使用中间
XmlDocument
执行两步反序列化过程,则需要按如下方式修改代码:

  • 在创建中间
    jsonText
    时使用省略根元素

    这将删除根
    “ProductionOrderFile”:{…}
    属性,该属性未出现在
    ProductionOrderFile
    中,并将嵌套属性冒泡到顶层

  • 按照中的说明,强制将
    元素转换为JSON数组

  • 因此,您的代码变成:

    private ProductionOrderFile ParseProductionOrderFile(Stream inputStream)
    {
        var serializer = new JsonSerializer();
        XmlDocument doc = new XmlDocument();
        doc.Load(inputStream);
    
        foreach (var xPath in new [] { "//Batches", "//Levels", "//VariableData" })
        {
            foreach (var node in doc.SelectNodes(xPath).Cast<XmlElement>())
            {
                node.SetAttribute("xmlns:json", "http://james.newtonking.com/projects/json");
                node.SetAttribute("Array", "http://james.newtonking.com/projects/json", XmlConvert.ToString(true));
            }
        }
    
        string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true);
    
        return JsonConvert.DeserializeObject<ProductionOrderFile>(jsonText);
    }
    
    private ProductionOrderFile ParseProductionOrderFile(流输入流)
    {
    var serializer=new JsonSerializer();
    XmlDocument doc=新的XmlDocument();
    单据加载(inputStream);
    foreach(新[]{”//Batches“,“//Levels”,“//VariableData”}中的var xPath)
    {
    foreach(doc.SelectNodes(xPath.Cast()中的var节点)
    {
    SetAttribute(“xmlns:json”http://james.newtonking.com/projects/json");
    node.SetAttribute(“数组”http://james.newtonking.com/projects/json,XmlConvert.ToString(true);
    }
    }
    字符串jsonText=JsonConvert.SerializeXmlNode(doc,Newtonsoft.Json.Formatting.None,true);
    返回JsonConvert.DeserializeObject(jsonText);
    }
    

    示例。

    如果您的目标是创建一个对象,为什么要将其转换为JSON?只需使用LINQ转换为XML或类似工具即可直接转换XML。@JonSkeet我之所以这样做,是因为我想使用JSON文件和XML文件。问题是:XML的格式是否正确?我仍然不明白为什么在其他地方你想使用JSON而被迫将XML转换为JSON。我突然想到,如果你不这样做,你会没事的——因为你可能在其他地方使用JSON,而JSON没有额外的根元素。
    {
        "ProductionOrderFile": {
            "ProductionOrderName": "\"ProOrd_Xml_001\"",
            "ProductCode": "Pro_EU_001",
            "Batches": {
                "Name": "Lote_Xml_01"
            },
            "Levels": [{
                "Id": "1",
                "Name": "Nivel_1",
                "PkgRatio": "120"
            }, {
                "Id": "2",
                "Name": "Nivel_2",
                "PkgRatio": "1"
            }],
            "VariableData": [{
                "VariableDataId": "01",
                "LevelId": "1",
                "Value": "Pro_EU_001"
            }, {
                "VariableDataId": "20",
                "LevelId": "1",
                "Value": "Lote_Xml_01"
            }, {
                "VariableDataId": "11",
                "LevelId": "1",
                "Value": "170101"
            }, {
                "VariableDataId": "17",
                "LevelId": "1",
                "Value": "210101"
            }, {
                "VariableDataId": "21",
                "LevelId": "1",
                "Value": "####################"
            }]
        }
    }
    
    public class ProductionOrderFile
    {
        public string ProductionOrderName { get; set; }
        public string ProductCode { get; set; }
        [System.Xml.Serialization.XmlElement]
        public List<Batch> Batches { get; set; }
        [System.Xml.Serialization.XmlElement]
        public List<AggregationLevelConfiguration> Levels { get; set; }
        [System.Xml.Serialization.XmlElement]
        public List<VariableData> VariableData { get; set; }
    }
    
    private ProductionOrderFile ParseProductionOrderFile(Stream inputStream)
    {
        var serializer = new XmlSerializer(typeof(ProductionOrderFile));
        return (ProductionOrderFile)serializer.Deserialize(inputStream);
    }
    
    private ProductionOrderFile ParseProductionOrderFile(Stream inputStream)
    {
        var serializer = new JsonSerializer();
        XmlDocument doc = new XmlDocument();
        doc.Load(inputStream);
    
        foreach (var xPath in new [] { "//Batches", "//Levels", "//VariableData" })
        {
            foreach (var node in doc.SelectNodes(xPath).Cast<XmlElement>())
            {
                node.SetAttribute("xmlns:json", "http://james.newtonking.com/projects/json");
                node.SetAttribute("Array", "http://james.newtonking.com/projects/json", XmlConvert.ToString(true));
            }
        }
    
        string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true);
    
        return JsonConvert.DeserializeObject<ProductionOrderFile>(jsonText);
    }