本文介绍了读取XML数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述








我有以下XML文件....当我跑,它只读公司姓名不是所有参数

我必须转移到后代。



Hi


I have Following XML File.... When I run, It Reads Only "Company Name" not All Parameters
I have to move on to descendants.

<CompanyName>RSJ TRADING PTY LTD T/AS BM GU</CompanyName>
                <BusinessName>RSJ TRADING PTY LTD T/AS BM GU</BusinessName>
                <Address>
                    <Address1>700 WATERDALE RD</Address1>
                    <Address2/>
                    <Suburb>BANDURA</Suburb>
                    <State>VIC</State>
                    &4t;Postcode>3083</Postcode>
                </Address>
            </CompanyDetails>
            <ContactDetails>

                <ContactName>JACQUELINE MAO</ContactName>
                <PhoneNumber>0394585893</PhoneNumber>
                <MobileNumber>0418885185</MobileNumber>





这是按钮获取数据下的代码







This is code under Button "Get Data"


XmlReader xmlRdr = XmlReader.Create(@"E:\Form Data.xml");

        while (xmlRdr.Read())
        {
            if (xmlRdr.Name == "CompanyName")
            {
                Label1.Text = xmlRdr.ReadString();
            }

            if (xmlRdr.Name == "AccountNumber")
            {
              Label2.Text = xmlRdr.ReadString();
            }
            if (xmlRdr.Name == "Address1")
            {
                Label3.Text = xmlRdr.ReadString();
            }
            if (xmlRdr.Name == "Suburb")
            {
                Label4.Text = xmlRdr.ReadString();
            }
            if (xmlRdr.Name == "State")
            {
            Label7.Text = xmlRdr.ReadString();
            }
            if (xmlRdr.Name == "Postcode")
            {
              Label8.Text = xmlRdr.ReadString();
            }







如何使用以下代码链接上面的代码,以便它能够读取和显示所有参数




How can link above code with following code so that it reads and display all parameters

using (XmlReader reader = XmlReader.Create("2books.xml")) {

  // Move the reader to the second book node.
  reader.MoveToContent();
  reader.ReadToDescendant("book");
  reader.Skip(); //Skip the first book.

  // Parse the file starting with the second book node.
  do {
     switch (reader.NodeType) {
        case XmlNodeType.Element:
           Console.Write("<{0}", reader.Name);
           while (reader.MoveToNextAttribute()) {
               Console.Write(" {0}='{1}'", reader.Name, reader.Value);
           }
           Console.Write(">");
           break;
        case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
        case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
     }
  }  while (reader.Read());

}

推荐答案

using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

namespace Test
{
    [Serializable]
    [XmlRoot("RootElement")]
    public class RootElement
    {
        [XmlElement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public class Test
    {
        public void setXmlValues()
        {
//============= Write values to xml file ===============
            RootElement testOut = new RootElement {
                                                    Id = 1,
                                                    Name = "Customer Name",
                                                    Address = "Customer Address"
                                                    };

            xml_serialise(testOut);

//============== Read the values from xml ==========
            RootElement testIn = xml_deserialise();
            int id = testIn.Id;
            string Name = testIn.Name;
            string address = testIn.Address;

        }


        private void xml_serialise(RootElement test)
        {
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));


            using (TextWriter writer = new StreamWriter("test.xml"))
            {
                ser.Serialize(writer, test);
            }
        }

        private RootElement xml_deserialise()
        {
            XmlSerializer ser = new XmlSerializer(typeof(RootElement));

            RootElement test;

            using (TextReader writer = new StreamReader("test.xml"))
            {
                test = (RootElement)ser.Deserialize(writer);
            }

            return test;
        }
    }
}







问候

Jegan




Regards
Jegan


这篇关于读取XML数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:06