【问题标题】:how to reading xml child nodes in foreach loop cc#如何在 foreach 循环 cc# 中读取 xml 子节点
【发布时间】:2021-03-18 03:45:30
【问题描述】:

我想读取 xml 中的所有节点。这是 xml:

<CreGetSuppliersResponse xmlns="http://www.pronto.net/cre/1.0.0">
  <APIResponseStatus>
    <Code>OK</Code>
  </APIResponseStatus>
  <Suppliers>
    <Supplier>
      <ABN></ABN>
      <Address1>ddd</Address1>
      <Address2>dd</Address2>
      <Address3>dd</Address3>
      <Address4></Address4>
      <AddressCountryCode>233</AddressCountryCode>
      <ContactName>dd</ContactName>
      <CurrencyCode>dd</CurrencyCode>
      <Email></Email>
      <PayToCode>dd</PayToCode>
      <PhoneNo>0262367513</PhoneNo>
      <SupplierCode>dd</SupplierName>
    </Supplier>
    <Supplier>
      <ABN></ABN>
      <Address1>dd</Address1>
      <Address2>dd</Address2>
      <Address3>d</Address3>
      <Address4>d</Address4>
      <AddressCountryCode>027</AddressCountryCode>
      <ContactName>d</ContactName>
      <CurrencyCode>dd</CurrencyCode>
      <Email></Email>
      <PayToCode>d</PayToCode>
      <PhoneNo>d</PhoneNo>
      <SupplierCode>dd</SupplierCode>
      <SupplierName>dd</SupplierName>
    </Supplier>
</Suppliers>
</CreGetSuppliersResponse >

文档 XmlDocument 文档 = 新 XmlDocument(); doc.LoadXml(responseStream);

我知道它并不多。但似乎无法找到我可以正确使用的信息。去过 MSDN 并试图从那里弄清楚,但没有结果。

感谢我能得到的所有帮助。

【问题讨论】:

标签: c# xml


【解决方案1】:

尝试xml序列化:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(CreGetSuppliersResponse));
            CreGetSuppliersResponse response = (CreGetSuppliersResponse)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(Namespace = "http://www.pronto.net/cre/1.0.0")]
    public class CreGetSuppliersResponse
    {
        [XmlArray("Suppliers")]
        [XmlArrayItem("Supplier")]
        public List<Supplier> Supplier { get; set; }

        public APIResponseStatus APIResponseStatus { get; set; } 
    }
    public class APIResponseStatus
    {
        public string Code { get; set; }
    }
    public class Supplier
    {
        public string ABN { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string Address3 { get; set; }
        public string Address4 { get; set; }
        public int AddressCountryCode { get; set; }
        public string ContactName { get; set; }
        public string CurrencyCode { get; set; }
        public string Email { get; set; }
        public string PayToCode { get; set; }
        public string PhoneNo { get; set; }
        public string SupplierName { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    相关资源
    最近更新 更多