如何使用 .net 将 xml 内容中的列表内容转换为列表?将 InstallmentList 中的 OtsInstallmentsDetails 元素分配到列表

How can I convert the content of a list in xml content into a list using .net?Assigning OtsInstallmentsDetails elements in InstallmentList to the list

提问人:Hasan 提问时间:11/6/2023 最后编辑:dbcHasan 更新时间:11/7/2023 访问量:34

问:

下面是 XML:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:soap=\"">
    <soap:Body>
        <ControlApplicationsResponse xmlns=\"http://FSM\">
            <ControlApplicationsResult>
                <ErrorMessage xmlns=\"">kaydı var</ErrorMessage>
                <ResponseCode xmlns=\"http://core.intertech.com.tr/\">02</ResponseCode>
                <InstallmentList xmlns=\"">
                    <OtsInstallmentsDetails>
                        <SubscriberNumber>53326</SubscriberNumber>
                        <SubscriberName>ergesrg</SubscriberName>
                        <InvoiceDate>2023-09-22T16:13:00</InvoiceDate>
                        <InvoiceNumber>0001032087</InvoiceNumber>
                        <InstallmentExplanation>1 Taksit</InstallmentExplanation>
                        <InstallmentSeqNumber>1</InstallmentSeqNumber>
                        <PaymentDate>2023-09-25T00:00:00</PaymentDate>
                        <PaymentAmount>1000.00</PaymentAmount>
                        <InstallmentAmount>0.00</InstallmentAmount>
                    </OtsInstallmentsDetails>

还有我的代码:

string xmlData = result;  // The XML shown above

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlData);

XmlNodeList installmentList = xmlDoc.ChildNodes[1].FirstChild.FirstChild.FirstChild.ChildNodes;

List<OtsInstallment> installments = new List<OtsInstallment>();

foreach (XmlNode installment in installmentList)
{
    if (installment.Name == "OtsInstallmentsDetails")
    {
        OtsInstallment installmentt = new OtsInstallment
        {
            SubscriberNumber = installment["SubscriberNumber"].InnerText,
            SubscriberName = installment["SubscriberName"].InnerText,
            InvoiceDate = DateTime.Parse(installment["InvoiceDate"].InnerText),
            InvoiceNumber = installment["InvoiceNumber"].InnerText,
            InstallmentExplanation = installment["InstallmentExplanation"].InnerText,
            InstallmentSeqNumber = int.Parse(installment["InstallmentSeqNumber"].InnerText),
            PaymentDate = DateTime.Parse(installment["PaymentDate"].InnerText),
            PaymentAmount = decimal.Parse(installment["PaymentAmount"].InnerText),
            InstallmentAmount = decimal.Parse(installment["InstallmentAmount"].InnerText)
        };
        installments.Add(installmentt);
    }
}
C# XML ASP.NET-MVC

评论

0赞 Serge 11/6/2023
如果你想获得帮助,你也必须发布你的 Xml 和你的类
1赞 Panagiotis Kanavos 11/6/2023
没有理由逐个读取节点。使用 DataContractSerializer 或 XmlSerializer 将整个文档直接反序列化为对象。事实上,如果您拥有服务的 WSDL 和 XSD 文档,甚至它们的 URL,则 .NET 可以为该服务生成完整的 SOAP 客户端

答: 暂无答案