1

I have been reading How To Parse XML In .NET Core There they show an example on parsing XML using XMLSerializer.

[XmlRoot("MyDocument", Namespace = "http://www.dotnetcoretutorials.com/namespace")]
public class MyDocument
{
    public string MyProperty { get; set; }

    public MyAttributeProperty MyAttributeProperty { get; set; }

    [XmlArray]
    [XmlArrayItem(ElementName = "MyListItem")]
    public List MyList { get; set; }
}

public class MyAttributeProperty
{
    [XmlAttribute("value")]
    public int Value { get; set; }
}

and to read it:

using (var fileStream = File.Open("test.xml", FileMode.Open))
{
    XmlSerializer serializer = new XmlSerializer(typeof(MyDocument));
    var myDocument = (MyDocument)serializer.Deserialize(fileStream);

    Console.WriteLine($"My Property : {myDocument.MyProperty}");
    Console.WriteLine($"My Attribute : {myDocument.MyAttributeProperty.Value}");

    foreach(var item in myDocument.MyList)
    {
        Console.WriteLine(item);
    }
}

In the code above itreads the local xml file:

using (var fileStream = File.Open("test.xml", FileMode.Open)).

I want to read an XML file from URL, and make use of XmlSerializer, how would I accomplish this?

O S
  • 169
  • 1
  • 7
  • Hello there. Can you check this : https://stackoverflow.com/questions/12240857/how-to-get-content-from-file-from-this-url Maybe it will be usefull – AntiqTech Jan 29 '21 at 22:22
  • Also this one about StreamReader: https://www.luisquintanilla.me/2017/12/18/read-file-from-url-dotnet-core/ – AntiqTech Jan 29 '21 at 22:25
  • A bit of terminology: *parsing* XML is the process of turning lexical XML (with angle brackets) into a tree representation. *Serialization* is the reverse process, turning the tree representation into lexical XML with angle brackets. So the idea of "parsing using a serializer" doesn't make much sense. – Michael Kay Jan 30 '21 at 08:32

1 Answers1

2

Since you already have your XML parsing logic in place, all you need is to swap out the file reading for an HTTP request.

using (var client = new HttpClient())
{
    var content = await client.GetStreamAsync("http://...");

    XmlSerializer serializer = new XmlSerializer(typeof(MyDocument));
    var myDocument = (MyDocument)serializer.Deserialize(new MemoryStream(content));

    Console.WriteLine($"My Property : {myDocument.MyProperty}");
    Console.WriteLine($"My Attribute : {myDocument.MyAttributeProperty.Value}");

    foreach(var item in myDocument.MyList)
    {
        Console.WriteLine(item);
    }
}
Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43
  • `var myDocument = (MyDocument)serializer.Deserialize(content);` says: `Cannot resolve method 'Deserialize(string)', candidates are: object Deserialize(System.IO.Stream) (in class XmlSerializer) object Deserialize(System.IO.TextReader) (in class XmlSerializer) object Deserialize(System.Xml.XmlReader) (in class XmlSerializer` – O S Jan 29 '21 at 22:29
  • `client.GetStreamAsync(url)` works tho. Update the answer and I will accept it. – O S Jan 29 '21 at 22:31
  • I see. I haven't testing it, but all you need to do is turn your content (string) into a Stream. I'll update the answer. – Slava Knyazev Jan 29 '21 at 22:33
  • Just nitpicking. There is a missing ) after `new MemoryStream(content)`. I can't edit only one char. – G Wimpassinger Jan 29 '21 at 22:49