XML 转换为 Json 和 DeserializeObject 问题 - InfoPath 名称空间?

XML conversion to Json and DeserializeObject issue - InfoPath namespaces?

我正在考虑将大量 InfoPath XML 文件作为 Json 文件加载到 Cosmos DB 中以测试可能的项目。但我正在努力将 XML 文件转换为 Json,然后重新加载到 c# class。我认为这与 InfoPath 放入 XML 的命名空间有关,但我不确定。起初我想我应该尝试从 XML 中删除所有命名空间引用,但现在我想知道我是否只需要在 class 文件的定义上做更多的工作。为了生成 class 文件,我使用了 Visual Studio "paste as Json"。我假设在加载 XML、保存到 Json 之后,然后将其用作新 class 文件的基础,我可以直接加载回对象,但是当我调用 DeserializeObject 时,它总是最终为空。

这是因为 class 文件定义需要更改,还是我应该在转换为 Json 之前尝试清理 XML?

样本XML

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:myProject:-myXSD-2017-05-05T14-19-13" solutionVersion="1.0.0.2046" productVersion="16.0.0.0" PIVersion="1.0.0.0" href="https://myportal.sharepoint.com/sites/mySite/myProject/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<?mso-infoPath-file-attachment-present?>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2017-05-05T14:19:13" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
    <my:Admin>
        <my:Routing_Order>
            <my:Approver-1_Order>1</my:Approver-1_Order>
            <my:Approver-2_Order>5</my:Approver-2_Order>
            <my:Approver-3_Order>4</my:Approver-3_Order>
        </my:Routing_Order>
    </my:Admin>
    <my:Request_Status>Save as Draft</my:Request_Status>
    <my:Request_Type>CAPEX</my:Request_Type>
</my:myFields>

Class 文件

namespace xml_to_json
{

    public class Rootobject
    {
        public MyMyfields mymyFields { get; set; }
    }

    public class MyMyfields
    {
        public string xmlnsmy { get; set; }
        public string xmlnsxsi { get; set; }
        public string xmlnsxhtml { get; set; }
        public string xmlnspc { get; set; }
        public string xmlnsma { get; set; }
        public string xmlnsd { get; set; }
        public string xmlnsq { get; set; }
        public string xmlnsdfs { get; set; }
        public string xmlnsdms { get; set; }
        public string xmlnstns { get; set; }
        public string xmlnss1 { get; set; }
        public string xmlnshttp { get; set; }
        public string xmlnstm { get; set; }
        public string xmlnssoap { get; set; }
        public string xmlnssoapenc { get; set; }
        public string xmlnsmime { get; set; }
        public string xmlnssoap12 { get; set; }
        public string xmlnswsdl { get; set; }
        public string xmlnsxd { get; set; }
        public string xmllang { get; set; }
        public MyAdmin myAdmin { get; set; }
        public string myRequest_Status { get; set; }
        public string myRequest_Type { get; set; }
    }

    public class MyAdmin
    {
        public MyRouting_Order myRouting_Order { get; set; }
    }

    public class MyRouting_Order
    {
        public string myApprover1_Order { get; set; }
        public string myApprover2_Order { get; set; }
        public string myApprover3_Order { get; set; }
    }


}

控制台测试代码

namespace xml_to_json
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument myDoc = new XmlDocument();
            string sourcedir = @"C:\Users\mystuff\Downloads\test-clean\";
            string xmlfilein = @"test";
            string xmlfile = string.Concat(sourcedir, xmlfilein, ".xml");
            string jsonfileout = string.Concat(sourcedir, xmlfilein, ".json");
            myDoc.Load(xmlfile);
            string jsonText = JsonConvert.SerializeXmlNode(myDoc.LastChild);
            File.WriteAllText(jsonfileout, jsonText);

            Rootobject obj = new Rootobject();
            obj = JsonConvert.DeserializeObject<Rootobject>(jsonText);
            Console.WriteLine(obj.mymyFields.myRequest_Status);   
        }   
    }
}

使用 xml linq 尝试下面的代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement routingOrder = doc.Descendants().Where(x => x.Name.LocalName == "Routing_Order").FirstOrDefault();

            Dictionary<string, string> orders = routingOrder.Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string status = (string)doc.Descendants().Where(x => x.Name.LocalName == "Request_Status").FirstOrDefault();
            string requestType = (string)doc.Descendants().Where(x => x.Name.LocalName == "Request_Type").FirstOrDefault();
        }
    }
}

使用 jdweng 和 XDocument 的建议有助于清理 XML 文件,以便更轻松地过渡到 Json。下面是我用来清理文档的代码示例。

    XDocument newxdoc = XDocument.Load(xmlfile);
    newxdoc.DescendantNodes().Where(e => e.NodeType.ToString().StartsWith("ProcessingInstruction")).Remove();          
    string newstringdoc = RemoveAllNamespaces(newxdoc.ToString());
    newxdoc = XDocument.Parse(newstringdoc);