首页 文章

将mongoexport json转换为XML

提问于
浏览
0

我将mongoDb数据库中的数据导出到json文件中,需要将其解析为XML才能在我们的ETL中使用 .

这是我尝试用来解析我的json导出的一行的代码示例:

var json = @"{""_id"":{""$oid"":""592bbd86b029e62830c5020a""},""DraftNumber"":""A1B1CB8D"",""ProductRange"":""COMPREHENSIVE_HOME_INSURANCE"",""DraftStatus"":""QUOTATION_DRAFT"",""DraftLabel"":"""",""LastUpdateDate"":{""$date"":""2017-05-29T06:19:53.559Z""},""EndDate"":{""$date"":""2017-07-28T06:19:53.559Z""},""UserId"":""D900036"",""ProjectNumber"":""38764496"",""Identifier"":"""",""CurrencyCode"":""EUR"",""RenewalDate"":{""$date"":""0001-01-01T00:00:00.000Z""},""RenewalDay"":0,""RenewalMounth"":0,""CreationDate"":{""$date"":""2017-05-29T06:19:50.138Z""},""EffectiveHour"":""0"",""EffectiveMinute"":""0"",""HasAs"":[{""_t"":""AgreementHolder"",""_id"":{""$oid"":""000000000000000000000000""},""DistribCustomer"":{""isProspect"":true}}],""IsBasedOnProduct"":{""pricingType"":""DISCOUNT_RATE"",""pricingVersion"":""C""},""ActivityInAgreements"":[{""_t"":""AgreementRequest"",""_id"":{""$oid"":""000000000000000000000000""},""PremiumNature"":null}],""OriginalSubscriptionChannel"":""DIRECT"",""CurrentSubscriptionChannel"":""DIRECT"",""BusinessExpirationDate"":{""$date"":""0001-01-01T00:00:00.000Z""},""TechnicalExpirationDate"":{""$date"":""2017-08-27T06:19:53.559Z""},""IsEligibleToProposal"":true,""IneligibityReasonCodes"":[],""DematerialisationOfDocuments"":true}";

var doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

Console.WriteLine(doc);
Console.ReadKey();

我面临以下例外:

L'exception Newtonsoft.Json.JsonSerializationException n'apasétégéréeHResult= -2146233088 Message = JSON根对象有多个属性 . 根对象必须具有单个属性才能创建有效的XML文档 . 考虑指定DeserializeRootElementName . 路径'DraftNumber',第1行,第57位.Source = Newtonsoft.Json StackTrace:àNewtonsoft.Json.Converters.XmlNodeConverter.DeserializeNode(JsonReader reader,IXmlDocument document,XmlNamespaceManager manager,IXmlNode currentNode)àNewtonsoft.Json.Converters.XmlNodeConverter . ReadJson(JsonReader reader,Type objectType,Object existingValue,JsonSerializer serializer)àNewtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter,JsonReader reader,Type objectType,Object existingValue)àNewtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader) ,类型objectType,Boolean checkAdditionalContent)àNewtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)àNewtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type,JsonSerializerSettings settings)àNewtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type,JsonConverter [] converter)àNewtonsoft.Json .JsonConvert.DeserializeXmlNode(String value,String deserializeRootElementName,Boolean writeArrayAttribute)àNewtonsoft.Json.JsonConvert.DeserializeXmlNode(String value)àConsoleApplication3.Program.Main(String [] args)dans c:\ documents \ s638723 \ documents \ visual studio 2015 \ Projects \ ConsoleApplication3 \ Program.cs:ligne17àSystem.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)àSystem.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)àMicrosoft.VisualStudio . HostingProcess.HostProc.RunUsersAssembly()àSystem.Threading.ThreadHelper.ThreadStart_Context(Object state)àSystem.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)àSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)àSystem.Threading.ExecutionContext.Run(ExecutionContext execute) Context,ContextCallback回调,对象状态)àSystem.Threading.ThreadHelper.ThreadStart()InnerException:

更全局我想知道 mongoexport 导出的类型是否可被jsonconverter使用 .

1 回答

  • 0

    正如异常所述,JSON在顶层具有 multiple 属性 .

    只需定义 root ,如:

    using (StreamReader r = new StreamReader("Json_1.json"))
    {
        string json = r.ReadToEnd();
        var doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "root");
    }
    

    Output:

    <root>
        <_id>
            <_x0024_oid>592bbd86b029e62830c5020a</_x0024_oid>
        </_id>
        <DraftNumber>A1B1CB8D</DraftNumber>
        <ProductRange>COMPREHENSIVE_HOME_INSURANCE</ProductRange>
        <DraftStatus>QUOTATION_DRAFT</DraftStatus>
        <DraftLabel/>
        <LastUpdateDate>
            <_x0024_date>2017-05-29T06:19:53.559Z</_x0024_date>
        </LastUpdateDate>
        <EndDate>
            <_x0024_date>2017-07-28T06:19:53.559Z</_x0024_date>
        </EndDate>
        <UserId>D900036</UserId>
        <ProjectNumber>38764496</ProjectNumber>
        <Identifier/>
        <CurrencyCode>EUR</CurrencyCode>
        <RenewalDate>
            <_x0024_date>0001-01-01T00:00:00Z</_x0024_date>
        </RenewalDate>
        <RenewalDay>0</RenewalDay>
        <RenewalMounth>0</RenewalMounth>
        <CreationDate>
            <_x0024_date>2017-05-29T06:19:50.138Z</_x0024_date>
        </CreationDate>
        <EffectiveHour>0</EffectiveHour>
        <EffectiveMinute>0</EffectiveMinute>
        <HasAs>
            <_t>AgreementHolder</_t>
            <_id>
                <_x0024_oid>000000000000000000000000</_x0024_oid>
            </_id>
            <DistribCustomer>
                <isProspect>true</isProspect>
            </DistribCustomer>
        </HasAs>
        <IsBasedOnProduct>
            <pricingType>DISCOUNT_RATE</pricingType>
            <pricingVersion>C</pricingVersion>
        </IsBasedOnProduct>
        <ActivityInAgreements>
            <_t>AgreementRequest</_t>
            <_id>
                <_x0024_oid>000000000000000000000000</_x0024_oid>
            </_id>
            <PremiumNature />
        </ActivityInAgreements>
        <OriginalSubscriptionChannel>DIRECT</OriginalSubscriptionChannel>
        <CurrentSubscriptionChannel>DIRECT</CurrentSubscriptionChannel>
        <BusinessExpirationDate>
            <_x0024_date>0001-01-01T00:00:00Z</_x0024_date>
        </BusinessExpirationDate>
        <TechnicalExpirationDate>
            <_x0024_date>2017-08-27T06:19:53.559Z</_x0024_date>
        </TechnicalExpirationDate>
        <IsEligibleToProposal>true</IsEligibleToProposal>
        <DematerialisationOfDocuments>true</DematerialisationOfDocuments>
    </root>
    

相关问题