MVZ
MVZ

Reputation: 2250

FileUpload to load .XML

I was previously loading an XML file by using:

        XDocument doc = XDocument.Load("File.xml");
        Visit(doc.Root);

Now I want to load the file by using a FileUpload box:

        XmlDocument doc = new XmlDocument();
        doc.Load(FileUpload1.FileContent);
        Visit(doc.root);

But now I'm getting an error on "(doc.root)" . It says that "does not contain a definition for 'Root' and no extension method 'Root' accepting a first argument type". What am I doing wrong?

Upvotes: 2

Views: 1004

Answers (1)

Xtian Macedo
Xtian Macedo

Reputation: 835

That is because XmlDocument doesnt have a root property; The root of the XmlDocument is represented by the DocumentElement property of the object, in your case: doc.DocumentElement and since your method receives an XElement parameter as input you will need to convert the XmlElement to an XElement before passing it into your Visit() method. Use the function below for doing it.

  /// <summary>  
  /// Converts an XmlElement to an XElement.  
  /// </summary>  
  /// <param name="xmlelement">The XmlElement to convert.</param>  
  /// <returns>The equivalent XElement.</returns>  
  public static XElement ToXElement(XmlElement xmlelement)  
  {    
     return XElement.Load(xmlelement.CreateNavigator().ReadSubtree());  
  }

Then try calling in this way:

  Visit(ToXElement(doc.DocumentElement));

Upvotes: 2

Related Questions