在c#wpf中读取xml文件
我有一个xml文件,我想在文本文件中读取。它必须作为文本(而不是代码本身)输出。我现在拥有的:(在打开xml文件的按钮中)。我现在可以打开文件并将其放在文本框中,但作为导出,它显示为代码,而不显示我想要查看的计算在c#wpf中读取xml文件,c#,xml,visual-studio,C#,Xml,Visual Studio,我有一个xml文件,我想在文本文件中读取。它必须作为文本(而不是代码本身)输出。我现在拥有的:(在打开xml文件的按钮中)。我现在可以打开文件并将其放在文本框中,但作为导出,它显示为代码,而不显示我想要查看的计算 XmlDocument xmldoc = new XmlDocument(); XmlNodeList xmlnode; int i = 0; string str = null; OpenFileDialog openF
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode;
int i = 0;
string str = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
tbBerekeningen.Text = File.ReadAllText(openFileDialog.FileName);
xmldoc.Load(tbBerekeningen.Text);
xmlnode = xmldoc.GetElementsByTagName("Product");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
MessageBox.Show(str);
XmlDocument xmldoc=new XmlDocument();
XmlNodeList xmlnode;
int i=0;
字符串str=null;
OpenFileDialog OpenFileDialog=新建OpenFileDialog();
if(openFileDialog.ShowDialog()==true)
tbBerekeningen.Text=File.ReadAllText(openFileDialog.FileName);
Load(tbBerekeningen.Text);
xmlnode=xmldoc.GetElementsByTagName(“产品”);
对于(i=0;i请尝试以下代码:
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode;
int i = 0;
string str = null;
string result = string.Empty;
List<int> Value1 = new List<int>();
List<int> Value2 = new List<int>();
List<string> Operator = new List<string>();
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
xmldoc.Load(openFileDialog.FileName);
xmlnode = xmldoc.GetElementsByTagName("Som");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
str = str + xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim() + System.Environment.NewLine;
Value1.Add(int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim()));
Value2.Add(int.Parse(xmlnode[i].ChildNodes.Item(2).InnerText.Trim()));
Operator.Add(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
}
tbBerekeningen.Text = str;
}
for (int j = 0; j < Value1.Count; j++)
{
if (Operator[j] == "+")
{
result = result + Value1[j] + Value2[j] + System.Environment.NewLine;
}
//add if else block or switch cases for all the operators.
//e.g if (Operator[j] == "-")
}
tbBerekeningen.Text = result;
XmlDocument xmldoc=new XmlDocument();
XmlNodeList xmlnode;
int i=0;
字符串str=null;
字符串结果=string.Empty;
列表值1=新列表();
列表值2=新列表();
列表运算符=新列表();
OpenFileDialog OpenFileDialog=新建OpenFileDialog();
if(openFileDialog.ShowDialog()==true)
{
Load(openFileDialog.FileName);
xmlnode=xmldoc.GetElementsByTagName(“Som”);
对于(i=0;i xmldoc.GetElementsByTagName(“Product”)。XML文件是否包含名为Product的元素?(您先前发布的XML不包含此元素)您需要更好地解释问题所在。我们不知道您需要什么计算。@Johan我编辑了文章,是的,我没有。但即使在输入“Som”时它仍然提供了整个xmlcode@jdweng请参阅edit>xml codeWell,它可以工作,谢谢,但是有没有办法在“tbCalculations”(不在messagebox中)中显示计算结果?编辑了我的代码示例。它现在将在“tbCalculations”中显示所有计算结果。太棒了!正如预期的那样!只是一个额外的问题:我如何让它计算数字?@user6840583:很高兴这起作用。我建议您将此标记为已接受的答案,并为一个新问题打开一个新的线程,因为它与发布的问题非常不同。我希望这会很好!谢谢!发布问题时请告诉我。我我会尽力帮你的!
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode;
int i = 0;
string str = null;
string result = string.Empty;
List<int> Value1 = new List<int>();
List<int> Value2 = new List<int>();
List<string> Operator = new List<string>();
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
xmldoc.Load(openFileDialog.FileName);
xmlnode = xmldoc.GetElementsByTagName("Som");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
str = str + xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim() + System.Environment.NewLine;
Value1.Add(int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim()));
Value2.Add(int.Parse(xmlnode[i].ChildNodes.Item(2).InnerText.Trim()));
Operator.Add(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
}
tbBerekeningen.Text = str;
}
for (int j = 0; j < Value1.Count; j++)
{
if (Operator[j] == "+")
{
result = result + Value1[j] + Value2[j] + System.Environment.NewLine;
}
//add if else block or switch cases for all the operators.
//e.g if (Operator[j] == "-")
}
tbBerekeningen.Text = result;