What is a quick way to search through xml

Suppose I have an XML file that I use as a local database, for example:

<root>
 <address>
  <firstName></firstName>
  <lastName></lastName>
  <phone></phone>
 </address>
</root>

I have a couple of questions:
1. What will be the fastest way to find the address (or addresses) in XML, where firstName contains "er", for example?
2. Is it possible to do without the integral loading of an XML file into memory?

PS I'm not looking for alternatives to XML files, ideally I need a search that does not depend on the number of addresses in the XML file. But I am a realist, and it seems to me that this is impossible.

Update: I am using .net 4
Thank you for the suggestions, but this is a more scientific task than practical. I'm probably looking for faster ways than linq and xmltextreader.

+3
source
6

LINQ to Xml :

XDocument doc = XDocument.Load("myfile.xml");
var addresses = from address in doc.Root.Elements("address")
                where address.Element("firstName").Value.Contains("er")
                select address;

: StackOverflow: xml .

, SQL:

-: xml? XmlDocument ""... "" .

-: (, SQL Server Express Edition), TSQL? , , xpath . , , SQL Server 2005 xml data-type, - xml DOM ( xpath ).

2: , , , XML : http://www.15seconds.com/issue/010410.htm

+7

.NET 3.5+, LINQ to XML.

, : ( / )

IEnumerable<string> addresses =
    from inv in customer.Descendants("Invoice")
    where inv.Attribute("ProductName").StartsWith("er")
    select (string) inv.Attribute("StreetAddress");
+2

XmlTextReader, . , , , .

+1

, , , , . ? , , .

, , , (n) . .

+1

, . XML memmory , ...

+1
source

What about XmlReader ? I think this may be the fastest way ...

I tried a file about 110 MB in size and it took about 1.1 sec. The same file with LinqToXML (above) takes about 3 seconds.

XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create("C:\\Temp\\items.xml", settings);

String firstName = "", lastName = "", phone = "";
String lastTagName = "";
Boolean bItemFound = false;
long nCounter = 0;

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

reader.MoveToContent();
// Parse the file and display each of the nodes.
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
            //Console.Write("<{0}>", reader.Name);

            lastTagName = reader.Name;

            if (lastTagName ==  "address")
                nCounter++;

            break;
        case XmlNodeType.Text:
            //Console.Write(reader.Value);
            switch (lastTagName)
            {
               case "firstName":
                    firstName = reader.Value.ToString();
                    bItemFound = firstName.Contains("97331");
                    break;
                case "lastName":
                    lastName = reader.Value.ToString();
                    break;
                case "phone":
                    phone = reader.Value.ToString();
                    break;
            }
            break;
        case XmlNodeType.CDATA:
            //Console.Write("<![CDATA[{0}]]>", reader.Value);
            break;
        case XmlNodeType.ProcessingInstruction:
            //Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
            break;
        case XmlNodeType.Comment:
            //Console.Write("<!--{0}-->", reader.Value);
            break;
        case XmlNodeType.XmlDeclaration:
            //Console.Write("<?xml version='1.0'?>");
            break;
        case XmlNodeType.Document:
        case XmlNodeType.DocumentType:
            //Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
            break;
        case XmlNodeType.EntityReference:
            //Console.Write(reader.Name);
            break;
        case XmlNodeType.EndElement:
            //Console.Write("</{0}>", reader.Name);
            break;
    }

    if (bItemFound)
    {
        Console.Write("{0}\n{1}\n{2}\n", firstName, lastName, phone);
        bItemFound = false;
    }
}

stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
Console.WriteLine("Searched items: {0}", nCounter);

Console.ReadKey();
+1
source

Source: https://habr.com/ru/post/1795849/


All Articles