В документе XML (2, 2) произошла ошибка при удалении из объекта xml до c# - PullRequest
0 голосов
/ 29 апреля 2020

XML

<?xml version="1.0" encoding="UTF-8"?>
<orgc:Organizations xmlns:orgc="urn:workday.com/connector/orgs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <orgc:Organization>
        <orgc:Organization_ID>SR1Code34</orgc:Organization_ID>
        <orgc:Organization_Code>SR1Code34</orgc:Organization_Code>
        <orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
        <orgc:Organization_Name>LTL Services</orgc:Organization_Name>
        <orgc:Organization_Description>LTL Services</orgc:Organization_Description>
        <orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
        <orgc:Inactive>false</orgc:Inactive>
        <orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
    </orgc:Organization>
    <orgc:Organization>
        <orgc:Organization_ID>SR1Code35</orgc:Organization_ID>
        <orgc:Organization_Code>SR1Code35</orgc:Organization_Code>
        <orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
        <orgc:Organization_Name>Consolidation</orgc:Organization_Name>
        <orgc:Organization_Description>Consolidation</orgc:Organization_Description>
        <orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
        <orgc:Inactive>false</orgc:Inactive>
        <orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
    </orgc:Organization>
</orgc:Organizations>

Класс

[XmlRoot(ElementName = "Organizations", Namespace = "urn: workday.com/connector/orgs", IsNullable = true )]
    public class CostCenterHierarchy
    {
        [XmlElement("orgc:Organization_ID")]
        public string CostCenterHierarchyId { get; set; }

        [XmlElement("orgc:Organization_Code")]
        public string Code { get; set; }

        [XmlElement("orgc:Organization_Name")]
        public string Name { get; set; }

        [XmlElement("orgc:Organization_Description")]
        public string Description { get; set; }

        [XmlElement("orgc:Organization_Subtype")]
        public string Subtype { get; set; }

        [XmlElement("orgc:Superior_Organization")]
        public string ParentHierarchyId { get; set; }
    }

Метод десерализации xml до c# Класс

private List<CostCenterHierarchy> ProcessCostCenterHierarchy(string filePath)
        {
            var costCenterHierarchyList = new List<CostCenterHierarchy>();
            //var costCenterHierarchy = new CostCenterHierarchy();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<CostCenterHierarchy>));
            using (var reader = XmlReader.Create(filePath))
            {
                var test = xmlSerializer.Deserialize(reader);
            }

            return costCenterHierarchyList;
        }

Сообщение об ошибке

Message = "There is an error in XML document (2, 2)."
InnerException = {"<Organizations xmlns='urn:workday.com/connector/orgs'> was not expected."}

Я не уверен, где я иду не так. Кажется, это должно быть довольно легко, но я поиграл с этим и продолжаю получать одно и то же сообщение об ошибке. Любая помощь приветствуется.

1 Ответ

0 голосов
/ 29 апреля 2020

Код ниже работает. У вас есть массив и сериализация не любит список в качестве типа. URL «urn: workday.com/connector/orgs», который не нравится сериализатору, и должен был заменить «urn:» на «http://workday.com/connector/orgs».

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;



namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Organizations));
            Organizations organizations = (Organizations)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "Organizations", Namespace = "http://workday.com/connector/orgs")]
    public class Organizations
    {
        [XmlElement(ElementName = "Organization", Namespace = "http://workday.com/connector/orgs")]
        public List<CostCenterHierarchy> organizations { get; set; }
    }
    public class CostCenterHierarchy
    {
        [XmlElement("Organization_ID")]
        public string CostCenterHierarchyId { get; set; }

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

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

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

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

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

}
...