【发布时间】:2018-10-26 22:26:03
【问题描述】:
我正在考虑将大量 InfoPath XML 文件作为 Json 文件加载到 Cosmos DB 中,以测试可能的项目。但我正在努力将 XML 文件转换为 Json,然后加载回 c# 类。我认为这与 InfoPath 放入 XML 的命名空间有关,但我不确定。起初我想我应该尝试从 XML 中删除所有命名空间引用,但现在我想知道我是否只需要在类文件的定义上做更多工作。为了生成类文件,我使用了 Visual Studio“粘贴为 Json”。我假设在加载 XML 后,保存到 Json,然后使用它作为新类文件的基础,我可以直接加载回对象,但是当我调用 DeserializeObject 时,它总是以 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
{
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);
}
}
}
【问题讨论】:
-
为什么不直接使用
XmlSerializer反序列化呢?如果您使用XmlSerializer,您可以完全跳过中间 JSON 表示。 -
如果我正确理解您的想法,目标不是将 XML 数据放入对象中。目标是将 XML 数据转换为 Json 格式,以便可以将其加载到 Cosmos DB 中,然后使用该 Json 文档作为对象进行测试。转换为 Json 不是我想跳过的步骤,而是我需要遵循的过程的一部分。
-
好的,那你为什么需要
Rootobject?一旦你有了jsonText,你已经将你的 XML 数据转换为 JSON 格式,你就完成了,对吧? -
RootObject 是通过我在第一篇文章中提到的粘贴到 Json 命令创建的。我曾希望 Visual Studio 可以为我做翻译。拥有一个 Json 文件是该步骤的一部分。我可以加载 Cosmos DB。然而,我的下一步将是读取该文件并将其加载到一个对象中,以便我可以测试为这些文档创建 UI。我研究得越多,我认为我需要在尝试转换之前删除 XML 中的命名空间和其他指令。
标签: c# json xml azure-cosmosdb infopath