Share via

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#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Timon Yang-MSFT 9,611 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.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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