C# 从c中的其他文件获取var#
我得到3个文件,我想使用彼此的变量值,一个文件是:C# 从c中的其他文件获取var#,c#,xml,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; } 我在您的示例中看到,xml
public class Dialogs
{
public Dictionary<string, Phrase> Phrases = new Dictionary<string, Phrase>();
}
public class Phrase
{
public string PhraseID = null;
public string Role = null;
}
我在您的示例中看到,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>
对于用法来说,应该是这样的:
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;
这不是有效的XML。必须只有一个根节点。变量
xml
在哪里定义?您当前的问题不清楚,代码似乎不完整,肯定无法按原样编译:(1)您没有为短语设置PhraseID
属性,并且不清楚该值应来自何处。(2) 您的LoadDialog
方法应该返回一个Dialogs
实例,而不是。(3) 对话框
类的词组
字典成员的键
是什么?因为我删掉了一些代码,所以前2个文件不好,可能假设dictionary Phrase.Role.Value={“2”、“1”、“1”、“2”、“1”、“2”},我想在第三个文件中创建一个var,应该怎么做,,,
<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;