正在尝试从 XML 文件读取列表以用于 DDT

Trying to read from XML File to List to use for DDT

我正在尝试读取这个名为 "clubregisterquick.xml" 的 xml 文件,然后尝试将其输出到列表中,这样我就可以 运行 测试多个数据。我不知道我哪里出错了,我的字段显示你是空调试。对 C# 很陌生

 <?xml version="1.0" encoding="utf-8"?>
<ClubRegisterQuick>
<Clubs>
<Club>
      <Email>Gambardella@hotmail.com</Email>
      <Clubname>Gambardella GAA</Clubname>
      <Clubphonenumber>07348555550</Clubphonenumber>
      <Firstname>David</Firstname>
      <Lastname>Peoples</Lastname>
     <Town>Maghera</Town>
     <Country>N.Ireland</Country>
    <Location>Down</Location>
    <Currency>GBP</Currency>
    <Password>password1</Password>
    <Clubtype>GAA</Clubtype>
</Club>
     <Club>
      <Email>Matthew@hotmail.com</Email>
      <Clubname>Computer GAA</Clubname>
      <Clubphonenumber>06855583733</Clubphonenumber>
      <Firstname>Paul</Firstname>
      <Lastname>Smyth</Lastname>
     <Town>Draperstown</Town>
     <Country>Ireland</Country>
     <Location>Wicklow</Location>
    <Currency>EUR</Currency>
    <Password>password1</Password>
    <Clubtype>Rugby</Clubtype>
</Club>
</Clubs>
</ClubRegisterQuick>

这是我试图将其实施到的代码。我还没有更改用于输入数据的文本,因为没有任何意义,因为我返回的是 null。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Collections;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Xml;


namespace ClassLibrary2
{

    [XmlRoot(ElementName = "ClubRegisterQuick")]
    public class Root
    {

        [XmlElement("Clubs")]
        public Clubs clubs { get; set; }

    }

    public class Clubs
    {

        public List<Club> clubs { get; set; }

    }

    public class Club
    {

        [XmlElement("Email")]
        public string Email { get; set; }
        [XmlElement("Clubname")]
        public string Clubname { get; set; }
        [XmlElement("Clubphonenumber")]
        public string Clubphonenumber { get; set; }
        [XmlElement("Firstname")]
        public string Firstname { get; set; }
        [XmlElement("Lastname")]
        public string Lastname { get; set; }
        [XmlElement("Town")]
        public string Town { get; set; }
        [XmlElement("Country")]
        public string Country { get; set; }
        [XmlElement("Location")]
        public string Location { get; set; }
        [XmlElement("PhonCurrencye")]
        public string Currency { get; set; }
        [XmlElement("Password")]
        public string Password { get; set; }
        [XmlElement("Clubtype")]
        public string Clubtype { get; set; }



    }
    public static class serialize
    {
        public static T Deserialize<T>(string path)
        {
            T result;
            using (var stream = File.Open(path, FileMode.Open))
            {
                result = Deserialize<T>(stream);
            }
            return result;
        }

        public static void Serialize<T>(T root, string path)
        {
            using (var stream = File.Open(path, FileMode.Create))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(stream, root);
            }
        }

        public static T Deserialize<T>(Stream stream)
        {

            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(stream);
        }




        [TestClass]
        public class RegisterClubQuick
        {
            private IWebDriver driver;
            private StringBuilder verificationErrors;
            private string baseURL;
            private bool acceptNextAlert = true;




            [TestInitialize]
            public void SetupTest()
            {
                driver = new FirefoxDriver();
                baseURL = "******";
                verificationErrors = new StringBuilder();
            }


            [TestCleanup]
            public void TeardownTest()
            {
                try
                {
                    driver.Quit();
                }
                catch (Exception)
                {
                    // Ignore errors if unable to close the browser
                }
                Assert.AreEqual("", verificationErrors.ToString());
            }

            [TestMethod]
            public void TheRegisterClubQuickTest()
            {
                var a = serialize.Deserialize<Root>("clubregisterquick.xml");






                    driver.Navigate().GoToUrl(baseURL + "/Clubs");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("//a[contains(text(),'Register your Club')]")).Click();
                    Thread.Sleep(3000);
                    driver.FindElement(By.Name("email")).Clear();
                    driver.FindElement(By.Name("email")).SendKeys("notebook@mailinator1.com");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//button[@type='submit'])[2]")).Click();
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//input[@name='clubName'])[2]")).Clear();
                    driver.FindElement(By.XPath("(//input[@name='clubName'])[2]")).SendKeys("NoteBook1 FC");
                    Thread.Sleep(3000);
                    new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[2]/select"))).SelectByText("Hurling");
                    Thread.Sleep(3000);
                    driver.FindElement(By.Name("firstName")).Clear();
                    driver.FindElement(By.Name("firstName")).SendKeys("Paul");
                    Thread.Sleep(3000);
                    driver.FindElement(By.Name("lastName")).Clear();
                    driver.FindElement(By.Name("lastName")).SendKeys("McDonnell");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//input[@name='clubPhoneNumber'])[2]")).Clear();
                    driver.FindElement(By.XPath("(//input[@name='clubPhoneNumber'])[2]")).SendKeys("07345656559");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//input[@name='town'])[2]")).Clear();
                    driver.FindElement(By.XPath("(//input[@name='town'])[2]")).SendKeys("CarrickFergus");
                    Thread.Sleep(3000);
                    new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[8]/select"))).SelectByText("N.Ireland");
                    Thread.Sleep(3000);
                    new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[9]/select"))).SelectByText("Armagh");
                    Thread.Sleep(3000);
                    new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[10]/select"))).SelectByText("GBP");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//input[@name='password'])[2]")).Clear();
                    driver.FindElement(By.XPath("(//input[@name='password'])[2]")).SendKeys("password1");
                    Thread.Sleep(3000);
                    driver.FindElement(By.Name("confirmPassword")).Clear();
                    driver.FindElement(By.Name("confirmPassword")).SendKeys("password1");
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//input[@type='checkbox'])[2]")).Click();
                    Thread.Sleep(3000);
                    driver.FindElement(By.XPath("(//button[@type='submit'])[4]")).Click();
                    Thread.Sleep(5000);

            }
            private bool IsElementPresent(By by)
            {
                try
                {
                    driver.FindElement(by);
                    return true;
                }
                catch (NoSuchElementException)
                {
                    return false;
                }
            }

            private bool IsAlertPresent()
            {
                try
                {
                    driver.SwitchTo().Alert();
                    return true;
                }
                catch (NoAlertPresentException)
                {
                    return false;
                }
            }

            private string CloseAlertAndGetItsText()
            {
                try
                {
                    IAlert alert = driver.SwitchTo().Alert();
                    string alertText = alert.Text;
                    if (acceptNextAlert)
                    {
                        alert.Accept();
                    }
                    else
                    {
                        alert.Dismiss();
                    }
                    return alertText;
                }
                finally
                {
                    acceptNextAlert = true;
                }
            }
        }
    }
}

您的 'Clubs' class 与 XML 结构不匹配。此外,我认为您需要将您的俱乐部 class 标记为可序列化。如果名称与元素匹配,则 XmlElement 属性([XmlElement("Password")] 等)是多余的。

我能够使用以下结构让您的示例工作:

[XmlRoot(ElementName="ClubRegisterQuick")]
public class Root
{
    public List<Club> Clubs { get; set; }
}

[Serializable]
public class Club
{
    public string Email { get; set; }
    public string Clubname { get; set; }
    public string Clubphonenumber { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Town { get; set; }
    public string Country { get; set; }
    public string Location { get; set; }
    public string Currency { get; set; }
    public string Password { get; set; }
    public string Clubtype { get; set; }
}

下面的代码有效。我发现的唯一错误是在 class 俱乐部中你需要 [XmlElement("Club")] 因为它是一个列表。如果没有 XmlElement,则会创建一组额外的标签,这使得它与您的 XML.

不兼容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Xml;



namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Stream stream = File.Open(FILENAME, FileMode.Open);
            ClassLibrary2.Root root = ClassLibrary2.serialize.Deserialize<ClassLibrary2.Root>(stream);
            stream.Close();

            ClassLibrary2.serialize.Serialize(root, FILENAME);
        }
    }
}
namespace ClassLibrary2
{

    [XmlRoot(ElementName = "ClubRegisterQuick")]
    public class Root
    {

        [XmlElement("Clubs")]
        public Clubs clubs { get; set; }

    }

    public class Clubs
    {

        [XmlElement("Club")]
        public List<Club> clubs { get; set; }

    }

    public class Club
    {

        [XmlElement("Email")]
        public string Email { get; set; }
        [XmlElement("Clubname")]
        public string Clubname { get; set; }
        [XmlElement("Clubphonenumber")]
        public string Clubphonenumber { get; set; }
        [XmlElement("Firstname")]
        public string Firstname { get; set; }
        [XmlElement("Lastname")]
        public string Lastname { get; set; }
        [XmlElement("Town")]
        public string Town { get; set; }
        [XmlElement("Country")]
        public string Country { get; set; }
        [XmlElement("Location")]
        public string Location { get; set; }
        [XmlElement("Currency")]
        public string Currency { get; set; }
        [XmlElement("Password")]
        public string Password { get; set; }
        [XmlElement("Clubtype")]
        public string Clubtype { get; set; }



    }
    public static class serialize
    {
        public static T Deserialize<T>(string path)
        {
            T result;
            using (var stream = File.Open(path, FileMode.Open))
            {
                result = Deserialize<T>(stream);
            }
            return result;
        }

        public static void Serialize<T>(T root, string path)
        {
            using (var stream = File.Open(path, FileMode.Create))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(stream, root);
                stream.Flush();
                stream.Close();
            }

        }

        public static T Deserialize<T>(Stream stream)
        {

            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(stream);

        }

    }
}
​