试图从XML文件读取到列表以用于滴滴涕

本文关键字:列表 用于 滴滴涕 读取 XML 文件 | 更新日期: 2023-09-27 18:14:28

我正试图读取这个xml文件,称为"clubregisterquick.xml",然后试图将其输出到一个列表,这样我就可以在测试中运行多个数据。我不知道我在哪里出错,我的字段显示u空调试。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;
                }
            }
        }
    }
}

试图从XML文件读取到列表以用于滴滴涕

您的'Clubs'类与XML结构不匹配。另外,我认为您需要将Club类标记为Serializable。如果名称与元素匹配,那么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; }
}

下面的代码可以工作。我发现唯一的错误是在类俱乐部,你需要[XmlElement("俱乐部")],因为它是一个列表。如果没有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);
        }
    }
}
​