Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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和反序列化对象问题-InfoPath命名空间?_C#_Json_Xml_Azure Cosmosdb_Infopath - Fatal编程技术网

C# XML转换为Json和反序列化对象问题-InfoPath命名空间?

C# XML转换为Json和反序列化对象问题-InfoPath命名空间?,c#,json,xml,azure-cosmosdb,infopath,C#,Json,Xml,Azure Cosmosdb,Infopath,我正在研究将大量InfoPath XML文件作为Json文件加载到Cosmos DB中,以测试可能的项目。但我正在努力将XML文件转换为Json,然后再加载回c#类。我认为这与InfoPath放入XML中的名称空间有关,但我不确定。起初我认为应该尝试从XML中删除所有名称空间引用,但现在我想知道是否需要在类文件的定义上做更多的工作。为了生成类文件,我使用了VisualStudio“粘贴为Json”。我假设在加载XML并保存为Json,然后将其用作新类文件的基础之后,我可以直接加载回对象中,但当我

我正在研究将大量InfoPath XML文件作为Json文件加载到Cosmos DB中,以测试可能的项目。但我正在努力将XML文件转换为Json,然后再加载回c#类。我认为这与InfoPath放入XML中的名称空间有关,但我不确定。起初我认为应该尝试从XML中删除所有名称空间引用,但现在我想知道是否需要在类文件的定义上做更多的工作。为了生成类文件,我使用了VisualStudio“粘贴为Json”。我假设在加载XML并保存为Json,然后将其用作新类文件的基础之后,我可以直接加载回对象中,但当我调用反序列化对象时,它总是以null结尾

这是因为类文件定义需要更改,还是应该在转换为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>
控制台测试代码

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);   
        }   
    }
}
namespace xml_to_json
{
班级计划
{
静态void Main(字符串[]参数)
{
XmlDocument myDoc=新的XmlDocument();
字符串sourcedir=@“C:\Users\mystuff\Downloads\test clean\”;
字符串xmlfilein=@“测试”;
string xmlfile=string.Concat(sourcedir,xmlfilein,“.xml”);
string jsonfileout=string.Concat(sourcedir,xmlfilein,“.json”);
加载myDoc.Load(xmlfile);
字符串jsonText=JsonConvert.SerializeXmlNode(myDoc.LastChild);
writealText(jsonfileout,jsonText);
Rootobject obj=新的Rootobject();
obj=JsonConvert.DeserializeObject(jsonText);
Console.WriteLine(obj.myfields.myRequest\u状态);
}   
}
}

使用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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XElement routingOrder=doc.subjects()。其中(x=>x.Name.LocalName==“路由顺序”).FirstOrDefault();
字典顺序=routingOrder.Elements()
.GroupBy(x=>x.Name.LocalName,y=>(字符串)y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串状态=(字符串)doc.subjections()。其中(x=>x.Name.LocalName==“请求状态”).FirstOrDefault();
string requestType=(string)doc.subjections()。其中(x=>x.Name.LocalName==“请求类型”).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);

我想我可以用XMLDocument做同样的事情,并将路径映射到每个节点,但我更希望找到一种方法,让反序列化对象在不单独映射每个节点的情况下工作。也许我会对原始问题进行编辑,以便在涉及名称空间时更加关注如何定义Json对象。这将说明类定义是否正确。然后你就会知道问题是类还是json。为什么不直接使用反序列化呢?如果您使用
XmlSerializer
,您可以完全跳过中间的JSON表示。如果我正确理解您的想法,那么目标不是将XML数据获取到对象中。目标是将XML数据转换为Json格式,以便将其加载到Cosmos DB中,然后使用该Json文档作为对象进行测试。转换为Json不是我想跳过的步骤,而是我需要遵循的过程的一部分。好吧,那么为什么需要
Rootobject
?一旦你有了
jsonText
你就把XML数据转换成了JSON格式,你就完成了,对吗?RootObject是从我在第一篇文章中提到的粘贴到JSON命令创建的。我曾希望VisualStudio能帮我翻译。拥有Json文件是该步骤的一部分。我可以加载宇宙数据库。然而,我的下一步是读取该文件并将其加载到一个对象中,以便测试为这些文档创建UI。我研究得越多,就越觉得在尝试转换之前需要删除XML中的名称空间和其他指令。
    XDocument newxdoc = XDocument.Load(xmlfile);
    newxdoc.DescendantNodes().Where(e => e.NodeType.ToString().StartsWith("ProcessingInstruction")).Remove();          
    string newstringdoc = RemoveAllNamespaces(newxdoc.ToString());
    newxdoc = XDocument.Parse(newstringdoc);