如何在C#ASP.Net中将json对象转换为所需的XML格式?

时间:2014-07-23 14:13:40

标签: c# asp.net xml json

您好我正在将我的json对象转换为字符串。 但节点的所有属性都转换为节点。

我希望字符串格式正确,请告诉我它是如何实现的。

Json对象:

                var RuleObj = {

    "Rule": {
        "RuleTypeID": {
            "RuleTypeValue": 105,
            "RWD_DEFAULT_SERVER_COMMISSION_PERCENTAGE": 5.75,
            "RWD_DEFAULT_SERVER_FIXED_COMMISSION_IN_PAISE": 123,
            "ME_DEFAULT_COMMISSION_PERCENTAGE": 5.75,
            "ME_DEFAULT_FIXED_COMMISSION_IN_PAISE": 123,
            "MAX_RULE_APPLICABLITY_PER_CARD_COUNT": 123,
            "Schemes": {
                "Schema": {
                    "MIN_BIN": 123,
                    "MAX_BIN": 999,
                    "RULE_START_HOUR": "2012-12-13T12:12:12",
                    "RULE_END_HOUR": "2017-12-13T12:12:12",
                    "APPLICABLE_DAYS_BITMAP": 127,
                    "MIN_AMOUNT": 123,
                    "MAX_AMOUNT": 999,
                    "RWD_SERVER_COMMISSION_PERCENTAGE": 5.75,
                    "RWD_SERVER_FIXED_COMMISSION_IN_PAISE": 123,
                    "ME_COMMISSION_PERCENTAGE": 5.75,
                    "ME_FIXED_COMMISSION_IN_PAISE": 123,
                    "INSTANT_DISCOUNT_PERCENTAGE_ISSUER": 5.7513433,
                    "INSTANT_DISCOUNT_FIXED_DISCOUNT_ISSUER_PAISE": 123,
                    "INSTANT_DISCOUNT_PERCENTAGE_MERCHANT": 5.75,
                    "INSTANT_DISCOUNT_FIXED_DISCOUNT_MERCHANT_PAISE": 123,
                    "MAX_INSTANT_DISCOUNT_AMOUNT_PAISE": 123,
                    "MIN_INSTANT_DISCOUNT_AMOUNT_PAISE": 123,
                    "INSTANT_DISCOUNT_STATIC_TEXT_HEADER": "str1234",
                    "INSTANT_DISCOUNT_STATIC_TEXT_FOOTER": "str1234",
                    "INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_HEADER": 123,
                    "INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_FOOTER": 123,
                    "NX_REWARD_MULTIPLIER": 123,
                    "NX_REWARD_FIXED_REWARD_POINTS": 123,
                    "MAX_REWARDED_POINTS": 123,
                    "MIN_REWARDED_POINTS": 123,
                    "NX_REWARD_STATIC_TEXT_HEADER": "str1234",
                    "NX_REWARD_STATIC_TEXT_FOOTER": "str1234",
                    "NX_REWARD_GRAPHIC_IMAGE_ID_HEADER": 123,
                    "NX_REWARD_GRAPHIC_IMAGE_ID_FOOTER": 123,
                    "EMI_MONTHS_BITMAP": 128,
                    "USE_ORIGINAL_AMOUNT_FOR_EMI_APPLICABILITY": 23,
                    "MAX_REDEEMED_COUNT": 123,
                    "MAX_SCHEMA_APPLICABLITY_PER_CARD_COUNT": 123,
                    "EMI_PARAMETERS": {
                        "EMI_PARAMETER": {
                            "EMI_TENURE_MONTH": "03",
                            "CUSTOMER_PROCESSING_FEE_PERCENTAGE": 5.75,
                            "SUBVENTION_PAYABLE_TO_ISSUER_PERCENTAGE": 5.75,
                            "INTEREST_RATE_PERCENTAGE": 5.75,
                            "MERCHANT_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE": 5.75,
                            "FIXED_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "MAX_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "MIN_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "ISSUER_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE": 5.75,
                            "FIXED_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "MAX_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "MIN_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE": 123,
                            "CASHBACK_TYPE": 12,
                            "STATIC_TEXT_HEADER": "str1234",
                            "STATIC_TEXT_FOOTER": "str1234",
                            "GRAPHIC_IMAGE_ID_HEADER": 123,
                            "GRAPHIC_IMAGE_ID_FOOTER": 123,
                            "PAYMENT_HOLIDAY": {
                                "MIN_BIN": 123,
                                "MAX_BIN": 100,
                                "MIN_AMOUNT": 123,
                                "MAX_AMOUNT": 999,
                                "HOLIDAY_PERIOD_DAYS": 123
                            }
                        }
                    }
                }
            }
        }
    }


            };
 var jsonText = $.toJSON(jsEmp);

使用Jquery和Ajax将JsonText从客户端传递到Asp.Net中的服务器端。

在服务器端我以这种方式将此Object转换为字符串:

 string json = new JavaScriptSerializer().Serialize(jsonParam);
 XmlDocument doc = JsonConvert.DeserializeXmlNode(json,"RuleSchema"); 
 string strTemp = doc.InnerXml;
 string strTempInnserText = doc.InnerText;

但是当我看到strTemp的价值时,就会发生这种情况。

     <RuleSchema>
 <Rule>
 <RuleTypeID>
  <RuleTypeValue>105</RuleTypeValue> 
  <RWD_DEFAULT_SERVER_COMMISSION_PERCENTAGE>5.75</RWD_DEFAULT_SERVER_COMMISSION_PERCENTAGE> 
  <RWD_DEFAULT_SERVER_FIXED_COMMISSION_IN_PAISE>123</RWD_DEFAULT_SERVER_FIXED_COMMISSION_IN_PAISE> 
  <ME_DEFAULT_COMMISSION_PERCENTAGE>5.75</ME_DEFAULT_COMMISSION_PERCENTAGE> 
  <ME_DEFAULT_FIXED_COMMISSION_IN_PAISE>123</ME_DEFAULT_FIXED_COMMISSION_IN_PAISE> 
  <MAX_RULE_APPLICABLITY_PER_CARD_COUNT>123</MAX_RULE_APPLICABLITY_PER_CARD_COUNT> 
 <Schemes>
<Schema>
  <MIN_BIN>123</MIN_BIN> 
  <MAX_BIN>999</MAX_BIN> 
  <RULE_START_HOUR>2012-12-13T12:12:12</RULE_START_HOUR> 
  <RULE_END_HOUR>2017-12-13T12:12:12</RULE_END_HOUR> 
  <APPLICABLE_DAYS_BITMAP>127</APPLICABLE_DAYS_BITMAP> 
  <MIN_AMOUNT>123</MIN_AMOUNT> 
  <MAX_AMOUNT>999</MAX_AMOUNT> 
  <RWD_SERVER_COMMISSION_PERCENTAGE>5.75</RWD_SERVER_COMMISSION_PERCENTAGE> 
  <RWD_SERVER_FIXED_COMMISSION_IN_PAISE>123</RWD_SERVER_FIXED_COMMISSION_IN_PAISE> 
  <ME_COMMISSION_PERCENTAGE>5.75</ME_COMMISSION_PERCENTAGE> 
  <ME_FIXED_COMMISSION_IN_PAISE>123</ME_FIXED_COMMISSION_IN_PAISE> 
  <INSTANT_DISCOUNT_PERCENTAGE_ISSUER>5.7513433</INSTANT_DISCOUNT_PERCENTAGE_ISSUER> 
  <INSTANT_DISCOUNT_FIXED_DISCOUNT_ISSUER_PAISE>123</INSTANT_DISCOUNT_FIXED_DISCOUNT_ISSUER_PAISE> 
  <INSTANT_DISCOUNT_PERCENTAGE_MERCHANT>5.75</INSTANT_DISCOUNT_PERCENTAGE_MERCHANT> 
  <INSTANT_DISCOUNT_FIXED_DISCOUNT_MERCHANT_PAISE>123</INSTANT_DISCOUNT_FIXED_DISCOUNT_MERCHANT_PAISE> 
  <MAX_INSTANT_DISCOUNT_AMOUNT_PAISE>123</MAX_INSTANT_DISCOUNT_AMOUNT_PAISE> 
  <MIN_INSTANT_DISCOUNT_AMOUNT_PAISE>123</MIN_INSTANT_DISCOUNT_AMOUNT_PAISE> 
  <INSTANT_DISCOUNT_STATIC_TEXT_HEADER>str1234</INSTANT_DISCOUNT_STATIC_TEXT_HEADER> 
  <INSTANT_DISCOUNT_STATIC_TEXT_FOOTER>str1234</INSTANT_DISCOUNT_STATIC_TEXT_FOOTER> 
  <INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_HEADER>123</INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_HEADER> 
  <INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_FOOTER>123</INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_FOOTER> 
  <NX_REWARD_MULTIPLIER>123</NX_REWARD_MULTIPLIER> 
  <NX_REWARD_FIXED_REWARD_POINTS>123</NX_REWARD_FIXED_REWARD_POINTS> 
  <MAX_REWARDED_POINTS>123</MAX_REWARDED_POINTS> 
  <MIN_REWARDED_POINTS>123</MIN_REWARDED_POINTS> 
  <NX_REWARD_STATIC_TEXT_HEADER>str1234</NX_REWARD_STATIC_TEXT_HEADER> 
  <NX_REWARD_STATIC_TEXT_FOOTER>str1234</NX_REWARD_STATIC_TEXT_FOOTER> 
  <NX_REWARD_GRAPHIC_IMAGE_ID_HEADER>123</NX_REWARD_GRAPHIC_IMAGE_ID_HEADER> 
  <NX_REWARD_GRAPHIC_IMAGE_ID_FOOTER>123</NX_REWARD_GRAPHIC_IMAGE_ID_FOOTER> 
  <EMI_MONTHS_BITMAP>128</EMI_MONTHS_BITMAP> 
  <USE_ORIGINAL_AMOUNT_FOR_EMI_APPLICABILITY>23</USE_ORIGINAL_AMOUNT_FOR_EMI_APPLICABILITY> 
  <MAX_REDEEMED_COUNT>123</MAX_REDEEMED_COUNT> 
  <MAX_SCHEMA_APPLICABLITY_PER_CARD_COUNT>123</MAX_SCHEMA_APPLICABLITY_PER_CARD_COUNT> 
<EMI_PARAMETERS>
<EMI_PARAMETER>
  <EMI_TENURE_MONTH>03</EMI_TENURE_MONTH> 
  <CUSTOMER_PROCESSING_FEE_PERCENTAGE>5.75</CUSTOMER_PROCESSING_FEE_PERCENTAGE> 
  <SUBVENTION_PAYABLE_TO_ISSUER_PERCENTAGE>5.75</SUBVENTION_PAYABLE_TO_ISSUER_PERCENTAGE> 
  <INTEREST_RATE_PERCENTAGE>5.75</INTEREST_RATE_PERCENTAGE> 
  <MERCHANT_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE>5.75</MERCHANT_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE> 
  <FIXED_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</FIXED_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <MAX_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</MAX_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <MIN_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</MIN_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <ISSUER_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE>5.75</ISSUER_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE> 
  <FIXED_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</FIXED_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <MAX_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</MAX_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <MIN_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE>123</MIN_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE> 
  <CASHBACK_TYPE>12</CASHBACK_TYPE> 
  <STATIC_TEXT_HEADER>str1234</STATIC_TEXT_HEADER> 
  <STATIC_TEXT_FOOTER>str1234</STATIC_TEXT_FOOTER> 
  <GRAPHIC_IMAGE_ID_HEADER>123</GRAPHIC_IMAGE_ID_HEADER> 
  <GRAPHIC_IMAGE_ID_FOOTER>123</GRAPHIC_IMAGE_ID_FOOTER> 
 <PAYMENT_HOLIDAY>
  <MIN_BIN>123</MIN_BIN> 
  <MAX_BIN>100</MAX_BIN> 
  <MIN_AMOUNT>123</MIN_AMOUNT> 
  <MAX_AMOUNT>999</MAX_AMOUNT> 
  <HOLIDAY_PERIOD_DAYS>123</HOLIDAY_PERIOD_DAYS> 
  </PAYMENT_HOLIDAY>
  </EMI_PARAMETER>
  </EMI_PARAMETERS>
  </Schema>
  </Schemes>
  </RuleTypeID>
  </Rule>
  </RuleSchema>

它应该采用以下格式:

    <RuleSchema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="RulesSchema.xsd">
  <Rule>
    <RuleTypeID RuleTypeValue="105" 
    RWD_DEFAULT_SERVER_COMMISSION_PERCENTAGE="5.75" 
    RWD_DEFAULT_SERVER_FIXED_COMMISSION_IN_PAISE="123" 
    ME_DEFAULT_COMMISSION_PERCENTAGE="5.75" 
    ME_DEFAULT_FIXED_COMMISSION_IN_PAISE="123" 
    MAX_RULE_APPLICABLITY_PER_CARD_COUNT="123">
      <Schemes>
        <Schema MIN_BIN="123" MAX_BIN="999" 
        RULE_START_HOUR="2012-12-13T12:12:12" 
        RULE_END_HOUR="2017-12-13T12:12:12" 
        APPLICABLE_DAYS_BITMAP="127" 
        MIN_AMOUNT="123" MAX_AMOUNT="999" 
        RWD_SERVER_COMMISSION_PERCENTAGE="5.75" 
        RWD_SERVER_FIXED_COMMISSION_IN_PAISE="123" 
        ME_COMMISSION_PERCENTAGE="5.75" 
        ME_FIXED_COMMISSION_IN_PAISE="123" 
        INSTANT_DISCOUNT_PERCENTAGE_ISSUER="5.7513433" 
        INSTANT_DISCOUNT_FIXED_DISCOUNT_ISSUER_PAISE="123" 
        INSTANT_DISCOUNT_PERCENTAGE_MERCHANT="5.75" 
        INSTANT_DISCOUNT_FIXED_DISCOUNT_MERCHANT_PAISE="123" 
        MAX_INSTANT_DISCOUNT_AMOUNT_PAISE="123" 
        MIN_INSTANT_DISCOUNT_AMOUNT_PAISE="123" 
        INSTANT_DISCOUNT_STATIC_TEXT_HEADER="str1234" 
        INSTANT_DISCOUNT_STATIC_TEXT_FOOTER="str1234" 
        INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_HEADER="123" 
        INSTANT_DISCOUNT_GRAPHIC_IMAGE_ID_FOOTER="123" 
        NX_REWARD_MULTIPLIER="123" 
        NX_REWARD_FIXED_REWARD_POINTS="123" 
        MAX_REWARDED_POINTS="123" 
        MIN_REWARDED_POINTS="123" 
        NX_REWARD_STATIC_TEXT_HEADER="str1234" 
        NX_REWARD_STATIC_TEXT_FOOTER="str1234" 
        NX_REWARD_GRAPHIC_IMAGE_ID_HEADER="123" 
        NX_REWARD_GRAPHIC_IMAGE_ID_FOOTER="123" 
        EMI_MONTHS_BITMAP="128" 
        USE_ORIGINAL_AMOUNT_FOR_EMI_APPLICABILITY="23" 
        MAX_REDEEMED_COUNT="123" 
        MAX_SCHEMA_APPLICABLITY_PER_CARD_COUNT="123">
          <EMI_PARAMETERS>
            <EMI_PARAMETER 
            EMI_TENURE_MONTH="03" 
            CUSTOMER_PROCESSING_FEE_PERCENTAGE="5.75" 
            SUBVENTION_PAYABLE_TO_ISSUER_PERCENTAGE="5.75" 
            INTEREST_RATE_PERCENTAGE="5.75" 
            MERCHANT_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE="5.75" 
            FIXED_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            MAX_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            MIN_MERCHANT_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            ISSUER_OFFERED_DISCOUNT_CASHBACK_PERCENTAGE="5.75" 
            FIXED_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            MAX_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            MIN_ISSUER_OFFERED_DISCOUNT_CASHBACK_AMOUNT_PAISE="123" 
            CASHBACK_TYPE="12" 
            STATIC_TEXT_HEADER="str1234" 
            STATIC_TEXT_FOOTER="str1234" 
            GRAPHIC_IMAGE_ID_HEADER="123" 
            GRAPHIC_IMAGE_ID_FOOTER="123">
              <PAYMENT_HOLIDAY 
              MIN_BIN="123" 
              MAX_BIN="100" 
              MIN_AMOUNT="123" 
              MAX_AMOUNT="999" 
              HOLIDAY_PERIOD_DAYS="123" />
            </EMI_PARAMETER>
          </EMI_PARAMETERS>
        </Schema>
      </Schemes>
    </RuleTypeID>
  </Rule>
</RuleSchema>

请告诉我,如果我做错了或有其他方法可以实现这一点。

1 个答案:

答案 0 :(得分:0)

嗨以下方式我们可以实现这一点,我们需要添加@before属性。

        string json = @"{ '?xml': { '@version': '1.0', '@standalone': 'no' }, 'root': { 'person': [ { '@id': '1',
     'name': 'Alan', 'url': 'http://www.google.com' }, { '@id': '2', 'name': 'Louis', 
     'url': 'http://www.yahoo.com' } ] } }";  
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); 

这是XML。

  <?xml version="1.0" standalone="no"?> 
    <root> 
    <person id="1"> 
    <name>Alan</name>
     <url>http://www.google.com</url>
         </person>
     <person id="2">
         <name>Louis</name> 
    <url>http://www.yahoo.com</url>
 </person> </root>