question

DikongPrigm-9942 avatar image
0 Votes"
DikongPrigm-9942 asked TimonYang-MSFT answered

Reading XML file

 <page number="1" width="2550" height="3300">
  <text X="2188" Y="162" Width="162" Height="47" Font="Z@R327A.tmp">3/5/2021</text>
  <text X="342" Y="329" Width="308" Height="47" Font="Z@R327A.tmp">Sergio R Aguirre</text>
  <text X="2163" Y="332" Width="153" Height="56" Font="Z@R327A.tmp">834.38</text>
  <text X="146" Y="424" Width="969" Height="56" Font="Z@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; }
     }


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.