outlaw1994
outlaw1994

Reputation: 267

loading a large xml file and comparing data

i have a large xml file and i want to search through the Desc nodes in it to see if any match the text a user inputs then if it does get the data from the code node directly underneath the Desc node this is an example of the xml im using except mine has around 1000 Script nodes in it

<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> 

this is the code i found online but it doesnt seem to work

    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; }

hope someone can help thanks

Upvotes: 0

Views: 108

Answers (1)

Wagner DosAnjos
Wagner DosAnjos

Reputation: 6374

Please try the following. I added using to properly dispose of the XmlTextReader after use.

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;
}


References

XmlTextReader Class

Upvotes: 1

Related Questions