C# 解析XML结构以执行特定操作
我必须递归地解析xml结构,并在遇到特定节点时执行一些操作。 对于这种情况,哪种设计模式或方法是好的 例:C# 解析XML结构以执行特定操作,c#,xml,C#,Xml,我必须递归地解析xml结构,并在遇到特定节点时执行一些操作。 对于这种情况,哪种设计模式或方法是好的 例: //一些数据 //一些数据 ... 我想解析整个xml,并针对特定步骤执行特定操作 例如,第1步执行加法,第2步执行sub等等,我将使用下面类似xml linq的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using
//一些数据
//一些数据
...
我想解析整个xml,并针对特定步骤执行特定操作
例如,第1步执行加法,第2步执行sub等等,我将使用下面类似xml linq的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication139
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement start = doc.Descendants("Start").FirstOrDefault();
foreach (XElement element in start.Elements())
{
switch (element.Name.LocalName)
{
case "step1" :
break;
case "step2":
break;
case "step3":
break;
case "step4":
break;
default:
break;
}
}
}
}
}
这正是XSLT的工作原理。只需定义与不同元素关联的规则,例如
<xsl:template match="step1">
<xsl:value-of select="@arg1 + @arg2"/>
</xsl:template>
当然,您需要提供更详细的信息才能使其具体化。请提出更具体的问题。到底是什么问题?你试过什么吗?什么编程语言?创建一个类,它可以保存xml数据并将xml反序列化到对象中,或者如果您不想反序列化,请使用XPathNavigator。
<xsl:template match="step1">
<xsl:value-of select="@arg1 + @arg2"/>
</xsl:template>