从c#中的其他文件中获取var

时间:2015-05-15 19:34:09

标签: c# xml

我得到3个文件,我想使用彼此的变量值,一个文件是:

public class Dialogs
{
    public Dictionary<string, Phrase> Phrases = new Dictionary<string, Phrase>();

}
public class Phrase
{
    public string PhraseID = null;
    public string Role = null;
}

第二个到这里:

public class DiaLoader
{
        public Dialogs LoadDialog()
    {
        // Variables
        List<Phrase> phrasesList = new List<Phrase>();
        Dialogs resultDialog = new Dialogs();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<Phrase  Role="2"></Phrase>
        <Phrase  Role="2"></Phrase>
        <Phrase  Role="1"></Phrase>
        <Phrase  Role="1"></Phrase>
        <Phrase  Role="2"></Phrase>
        <Phrase  Role="1"></Phrase>
        <Phrase  Role="2"></Phrase>");
    //get all the phrases
        XmlNodeList phrases = xml.GetElementsByTagName("Phrase");
            foreach (XmlNode phraseNode in phrases)
            {
                Phrase phrase = NodeToPhrase(phraseNode);
                phrasesList.Add(phrase);
            }
    //Phrase node to phrase
        private Phrase NodeToPhrase(XmlNode node)
        {
            Phrase phrase = new Phrase();
            XmlNode roleNode = node.Attributes["Role"];
            if (roleNode != null && !string.IsNullOrEmpty(roleNode.Value))
                phrase.Role = roleNode.Value;

            return phrase;
        }  
}

正如你所看到的,我在第二个文件中使用xml给出字符串Role值,第三个,我想创建var并获取Role的值,我该怎么做?我的代码在这里:

    DiaLoader dia= new DiaLoader();
    public void Export(dia.LoadDialog()) {
    XmlDocument doc = new XmlDocument();
    string myVar = Phrase.Role;//this can't the value of role
  }

1 个答案:

答案 0 :(得分:1)

我在你的例子中看到你在xml文件中有一些短语,如:

<Phrases>
    <Phrase Role="2">Example 1</Phrase>
    <Phrase Role="2">Example 2</Phrase>
    <Phrase Role="1">Example 3</Phrase>
    <Phrase Role="1">Example 4</Phrase>
    <Phrase Role="2">Example 5</Phrase>
    <Phrase Role="1">Example 6</Phrase>
    <Phrase Role="2">Example 7</Phrase>
</Phrases>

并且您希望将所有这些短语都读入字典,然后检索特定角色的文本。

所以,我修改了一下你的代码以便编译。

using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace Test001
{
    public class Dialogs
    {
        private static string DEFAULT_DATA =
            "<Phrases>" +
                "<Phrase Role=\"2\">Example 1</Phrase>" +
                "<Phrase Role=\"2\">Example 2</Phrase>" +
                "<Phrase Role=\"1\">Example 3</Phrase>" +
                "<Phrase Role=\"1\">Example 4</Phrase>" +
                "<Phrase Role=\"2\">Example 5</Phrase>" +
                "<Phrase Role=\"1\">Example 6</Phrase>" +
                "<Phrase Role=\"2\">Example 7</Phrase>" +
            "</Phrases>"
        ;

        private int nextID;

        private Dictionary<string, Phrase> Phrases = new Dictionary<string, Phrase>();

        public List<Phrase> PhrasesList
        {
            get
            {
                return this.Phrases.Values.ToList();
            }
        }


        public Dialogs()
        {
            this.Phrases = new Dictionary<string, Phrase>();
            this.nextID = 0;
        }

        public bool Load(string filename = null)
        {
            this.Phrases.Clear();
            this.nextID = 0;
            XmlDocument doc = new XmlDocument();
            try
            {
                if (filename == null)
                {
                    doc.LoadXml(DEFAULT_DATA);
                }
                else
                {
                    doc.Load(filename);
                }
            }
            catch
            {
                // Error loading data
                return false;
            }

            // Get all the phrases
            XmlNodeList phrases = doc.GetElementsByTagName("Phrase");
            foreach (XmlNode phraseNode in phrases)
            {
                Phrase phrase = NodeToPhrase(phraseNode);
                this.Add(phrase);
            }

            return true;
        }

        public void Add(Phrase phrase)
        {
            this.Phrases.Add(this.nextID.ToString(), phrase);
            this.nextID++;
        }

        // Parse a xml node to a phrase
        private Phrase NodeToPhrase(XmlNode node)
        {
            Phrase phrase = new Phrase();
            XmlNode roleNode = node.Attributes["Role"];
            if (roleNode != null && !string.IsNullOrEmpty(roleNode.Value))
            {
                phrase.Role = roleNode.Value;
                phrase.PhraseID = this.nextID.ToString();
                if (node.HasChildNodes)
                {
                    phrase.Text = node.FirstChild.Value;
                }
                this.nextID++;
            }

            return phrase;
        }
    }
}

除了添加了一个用于存储文本的新字段

之外,我没有保留类短语
public class Phrase
{
    public string PhraseID = null;
    public string Role = null;
    public string Text = null;
}

对于用法,它将是这样的:

Dialogs dia = new Dialogs();
// dia.Load("full_path_to_your_nice_xml_file.xml")
dia.Load();     // Load default xml data just for testing purposes
var myVar = dia.PhrasesList.Find(phrase => phrase.Role == "2").Text;