Reading XML file

DikongPrigm 61 Reputation points
2021-06-21T00:26:24.603+00:00
<page number="1" width="2550" height="3300">  
 <text X="2188" Y="162" Width="162" Height="47" Font="******@R327A.tmp">3/5/2021</text>  
 <text X="342" Y="329" Width="308" Height="47" Font="******@R327A.tmp">Sergio R Aguirre</text>  
 <text X="2163" Y="332" Width="153" Height="56" Font="******@R327A.tmp">834.38</text>  
 <text X="146" Y="424" Width="969" Height="56" Font="******@R327A.tmp">Eight hundred thirty-our and 38/100 Dollars</text>  
</page>  

How do you guys read an XML like this?
I do have a sample class but don't know how to use it.

    [XmlRoot(ElementName="text")]  
    public class Text {  
        [XmlAttribute(AttributeName="X")]  
        public string X { get; set; }  
        [XmlAttribute(AttributeName="Y")]  
        public string Y { get; set; }  
        [XmlAttribute(AttributeName="Width")]  
        public string Width { get; set; }  
        [XmlAttribute(AttributeName="Height")]  
        public string Height { get; set; }  
        [XmlAttribute(AttributeName="Font")]  
        public string Font { get; set; }  
    }  
      
    [XmlRoot(ElementName="page")]  
    public class Page {  
        [XmlElement(ElementName="text")]  
        public List<Text> Text { get; set; }  
        [XmlAttribute(AttributeName="number")]  
        public string Number { get; set; }  
        [XmlAttribute(AttributeName="width")]  
        public string Width { get; set; }  
        [XmlAttribute(AttributeName="height")]  
        public string Height { get; set; }  
    }  
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-06-21T01:38:41.167+00:00

    Try to use XmlSerializer to do it.

            static void Main(string[] args)  
            {  
                string path = @"C:\...\1.txt";  
                XmlSerializer serializer = new XmlSerializer(typeof(Page));  
                using (FileStream fileStream = new FileStream(path, FileMode.Open))  
                {  
                    Page result = (Page)serializer.Deserialize(fileStream);  
      
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.