将 XML 序列化为 POCO,然后序列化为 JSON 字符串

问题描述 投票:0回答:1

我有以下 XML,我需要将其序列化为 POCO,然后将其反序列化为 JSON。 这是 XML:

<PAC xmlns="https://mynamespace.com">
  <Creds>
    <AID>1019766</AID>
    <AToken>290D56096864849B</AToken>
    <AcID>01234</AcID>
  </Creds>
  <App>
    <AppID>133</AppID>
    <AppName>XML</AppName>
    <AppVer>1.1.1</AppVer>
  </App>
  <PAcct>
    <PAcctType>0</PAcctType>
    <PAcctRefNum>000001</PAcctRefNum>
  </PAcct>
  <CD>
    <CN>4445_220007</CN>
    <EM>12</EM>
    <EY>25</EY>
    <C3></C3>
  </CD>
</PAC>

这是我尝试从 XML 填充然后反序列化为 JSON 的对象。 CCS 和 PAC 是从 TObjects 派生的基础对象,其中包含所有其他对象。

[XmlRoot("CCS", Namespace = "https://mynamespace.com")]
public partial class CCS : TObjects
{
    public TTypeID TTypeID { get; } = TTypeID.CCS; // Enum for TTypes
}
[XmlRoot("PAC", Namespace = "https://mynamespace.com")]
public partial class PAC : TObjects
{
    public TTypeID TTypeID { get; } = TTypeID.PAC; // Enum for TTypes
}
public partial class TObjects
{
    public Creds? Creds { get; set; }
    public App? App { get; set; }
    public PAcct? PAcct { get; set; }
    public CD? CD { get; set; }
}
public partial class App
{
    public int? AppID { get; set; }
    public string? AppName { get; set; }
    public string? AppVer { get; set; }
}
public partial class Creds
{
    public int? AID { get; set; }
    public string? AToken { get; set; }
    public string? AcID { get; set; }
}
public partial class PAcct
{
    public int? PAcctID { get; set; }
    public string? PAcctType { get; set; }
    public string? PAcctRefNum { get; set; }
}
public partial class CD
{
    public string? T1 { get; set; }
    public string? T2 { get; set; }
    public string? CN { get; set; }
    public CL? CL { get; set; } // CL is Enum
    public int? EM { get; set; }
    public int? EY { get; set; }
    public int? C3 { get; set; }
}

我一直在这段代码中使用 Newtonsoft 的 SerilizeXmlNode() ;此代码在进行序列化之前删除名称空间。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(System.Text.RegularExpressions.Regex.Replace(xmlString.Trim(), @"(xmlns:?[^=]*=[""][^""]*[""])", "",
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline));
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc.FirstChild, Newtonsoft.Json.Formatting.Indented);

它会生成有效的 Json 字符串,但整数都是字符串,并且它无法识别 XML 中的空值。

我还需要能够传入其他 XML 并序列化为从 TObject 派生的相应对象,如上面的 CCS,然后反序列化为 Json。

如何将上面的 XML 转换为这个 Json 字符串并忽略空值并以类名作为根进行格式化?

{
  "PAC": {
    "Creds": {
      "AID": 1019766,
      "AToken": "290D56096864849B",
      "AcID": "01234"
    },
    "App": {
      "AppID": "133",
      "AppName": "XML",
      "AppVer": "1.1.1"
    },
    "PAcct": {
      "PAcctType": "0",
      "PAcctRefNum": "000001"
    },
    "Card": {
      "CN": "4445_220007",
      "EM": 12,
      "EY": 25,
    }
  }
}
c# xml-parsing json.net
1个回答
0
投票

有理由使用类作为插页式步骤吗?

没有插播类的选项

string xml = "<PAC xmlns=\"https://mynamespace.com\"><Creds><AID>1019766</AID><AToken>290D56096864849B</AToken><AcID>01234</AcID></Creds><App><AppID>133</AppID><AppName>XML</AppName><AppVer>1.1.1</AppVer></App><PAcct><PAcctType>0</PAcctType><PAcctRefNum>000001</PAcctRefNum></PAcct><CD><CN>4445_220007</CN><EM>12</EM><EY>25</EY><C3/></CD></PAC>";
     
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc).Dump();

结果:

{
    "PAC": {
        "@xmlns": "https://mynamespace.com",
        "Creds": {
            "AID": "1019766",
            "AToken": "290D56096864849B",
            "AcID": "01234"
        },
        "App": {
            "AppID": "133",
            "AppName": "XML",
            "AppVer": "1.1.1"
        },
        "PAcct": {
            "PAcctType": "0",
            "PAcctRefNum": "000001"
        },
        "CD": {
            "CN": "4445_220007",
            "EM": "12",
            "EY": "25",
            "C3": null
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.