简体   繁体   English

XML DeSerializer:从标签返回第一个单词,该标签的值为字符串,在C#中带有空格

[英]XML DeSerializer: returns first word from a tag whose value is string with spaces in C#

My Xml file has following values: 我的Xml文件具有以下值:

<?xml version="1.0"?>
<CASAuthXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Username>username</Username>
<Password>password</Password>
<ExpectedTitlePage>xml values with spaces</ExpectedTitlePage>
</CASAuthXML>

Below program retrieves the XML values from a xml file. 下面的程序从xml文件中检索XML值。

using System;
using System.IO;
using System.Xml.Serialization;
namespace Framework.Utils
{
    public class CASAuthXML
    {
        public string Username;
        public string Password;
        public string ExpectedTitlePage;
        public string GetCASCredentials()
        {
            CASAuthXML values = new CASAuthXML();
            XmlSerializer reader = new XmlSerializer(typeof(CASAuthXML));
            StreamReader file = new StreamReader(Directory.GetCurrentDirectory() + "//XMLFile.xml");
            values = (CASAuthXML)reader.Deserialize(file);
            file.Close();
            Username = values.Username;
            Password = values.Password;
            ExpectedTitlePage = values.ExpectedTitlePage;
            return Username + " " + Password + " " + ExpectedTitlePage;

        }

    }

} }

Here the method returns exact title page retrieved from the XML file. 在这里,该方法返回从XML文件检索的确切标题页。 But the problem is as the method returns strings spliting from the XML, the value of ExpectedTitlePage in code below is only xml , While I expect all xml values with spaces as value of ExpectedTitlePage. 但是问题在于,因为该方法返回从XML拆分的字符串,所以下面代码中的ExpectedTitlePage的值只是xml ,而我希望所有带有空格的xml值都作为ExpectedTitlePage的值。

public static void Authenticate(IWebDriver driver, string ExpectedTitlePage, string username, string password)`
{
string actualPageTitle = driver.Title;


        if (actualPageTitle != ExpectedTitlePage)
        {
            FatalError.OnError("Error authenticating. Web driver is currently at page: " + actualPageTitle, "CASError");
         }
        driver.FindElement(By.Id("username")).SendKeys(username);
        driver.FindElement(By.Id("password")).SendKeys(password);
        driver.FindElement(By.CssSelector("input.btnNormal")).Click();


}

How to return all strings to the authenticate class? 如何将所有字符串返回给authenticate类?

我建议您使用Xmlreader ,这将允许您使用空格获取xml元素,这里是一个很好的答案,向您展示了如何实现它: XmlReader StackOverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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