我有一个大的xml文件,我想搜索其中的Desc节点,看看是否与用户输入的文本匹配,如果它确实从Desc节点下面的代码节点获取数据,这就是一个例子除了我的xml im using大约有1000个Script节点
<Scripts>
<script>
<Name>mst</Name>
<Desc>Destroy 1 spell/trap on the field</Desc>
<code>function c????????.initial_effect(c)
--Activate
local e1=Effect.CreateEffect(c)
e1:SetCategory(CATEGORY_DESTROY)
e1:SetType(EFFECT_TYPE_ACTIVATE)
e1:SetCode(EVENT_FREE_CHAIN)
e1:SetHintTiming(0,TIMING_END_PHASE+TIMING_EQUIP)
e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
e1:SetTarget(c????????.target)
e1:SetOperation(c????????.activate)
c:RegisterEffect(e1)
end
function c????????.filter(c)
return c:IsDestructable() and c:IsType(TYPE_SPELL+TYPE_TRAP)
end
function c????????.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
if chkc then return chkc:IsOnField() and c????????.filter(chkc) end
if chk==0 then return Duel.IsExistingTarget(c????????.filter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,e:GetHandler()) end
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
local g=Duel.SelectTarget(tp,c????????.filter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,e:GetHandler())
Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
end
function c????????.activate(e,tp,eg,ep,ev,re,r,rp)
local tc=Duel.GetFirstTarget()
if tc:IsRelateToEffect(e) then
Duel.Destroy(tc,REASON_EFFECT)
end
end</code>
</script>
</Scripts>
这是我在网上找到的代码,但它似乎无法正常工作
public static string Xmlload(String node)
{
XmlTextReader script = new XmlTextReader("Scripts.xml");
while (script.Read())
{
if (script.NodeType == XmlNodeType.Element &&
script.LocalName == node &&
script.IsStartElement() == true)
{
ProcessRewardNode(script,node);
script.Skip();
return myID;
}
else
{
return null;
}
}
myID = "test";
return myID;
}
private static void ProcessRewardNode(XmlTextReader RewardReader, string node)
{
XmlDocument RewardXmlDoc = new XmlDocument();
RewardXmlDoc.LoadXml(RewardReader.ReadOuterXml());
// we can use xpath as below
myID = RewardXmlDoc.SelectSingleNode(node).InnerText;
}
public static string myID { get; set; }
希望有人可以帮助谢谢
答案 0 :(得分:1)
请尝试以下方法。我添加了using
以便在使用后正确处理XmlTextReader
。
void Main()
{
Console.WriteLine(Xmlload("Destroy 1 spell/trap on the field"));
}
public static string Xmlload(String textUserInputs)
{
using (var script = new XmlTextReader("Scripts.xml"))
{
while (script.Read())
{
if (script.NodeType == XmlNodeType.Element &&
script.LocalName == "Desc" &&
script.IsStartElement() == true)
{
var desc = script.ReadElementContentAsString();
if (desc == textUserInputs)
{
script.ReadToNextSibling("code");
return script.ReadElementContentAsString();
}
}
}
}
return null;
}