简体   繁体   中英

how to reading xml child nodes in foreach loop cc#

I want to read all the nodes in the xml.This is the 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 >

Document XmlDocument doc = new XmlDocument(); doc.LoadXml(responseStream);

I know its not much. But cant seem to find information I can use properly. Been to MSDN and tried to figure it out from there, but to no result.

Thankful for all the help I can get.

Try xml serilization:

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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM