Search code examples
c#asp.netxmllinqxmlreader

How to use IN with xmlreader


How to use IN(,,,) with xmlreader to get specific nodes.

private static IEnumerable<DayNode> ReadDayNodes(string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (XmlReader xrdr = new XmlTextReader(fs))
                while (xrdr.Read())
                    if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "day")
                        yield return new DayNode(xrdr.GetAttribute("name"), xrdr.GetAttribute("short"), xrdr.GetAttribute("day"));
        }

If i have list of string. like this :

List<string> names = new new List<string>();

and i wanna to get only the nodes which name in the previous list .How to that?


Solution

  • Do you mean, where the element's local-name isn't necessary "day", but rather is in the list?

    if(xrdr.NodeType == XmlNodeType.Element && names.Contains(xrdr.LocalName))
    

    Edit following on comments:

    if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "day")
      foreach(string localName in names)
      {
        string attVal = xrdr.GetAttribute(localName);
        if(attVal != null)
        //we could just yield return attVal, but presuming we want to know which attribute it was on:
         yield return Tuple.Create(localName, attVal);
      }