Home > Software engineering >  XMLReader return 'None' Visual Studio 2019 C#
XMLReader return 'None' Visual Studio 2019 C#

Time:10-21

I'm using XMLReader create to read xml file.By giving xmlfilepath as input it returns 'None'

      public void Publish(string input, string output)
      {
        XmlReaderSettings settings = new()
        {
            DtdProcessing = DtdProcessing.Parse
        };            
        using (XmlReader reader = XmlReader.Create(input,settings)))
        {
 
        }
     }
   //For example input: C:\XML\30003.xml

XMLReader returns 'None' !!

CodePudding user response:

You need to call the read function after "Create". reader.Read();

CodePudding user response:

You have to do something "interesting" with the reader you created.

As seen here:

https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreader.read?view=net-7.0#examples

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

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);
          break;
      case XmlNodeType.Text:
          Console.Write(reader.Value);
          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:
           break;
       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;
   }
}
  •  Tags:  
  • c#
  • Related