Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys, i'm trying to read this xml file on wcf, so i can use them to my soap services:


<?xml version="1.0" encoding="utf-8"?>
<Contagiados>
  <Infetado nome="José Antunes" datanasc="2000-01-29" cidade="Barcelos" nrutente="123123872" internado="Sim" localInternamento="Hospital de Barcelos" Datainternamento="">
    <IsolamentoProfilatico>
        <Isolado nome="Marcelo Sousa" datanasc="1989-02-29" cidade="Lisboa" nrutente="123213872">
          <COVID>Não</COVID>
        </Isolado>
        <Isolado nome="André Ventura" datanasc="1980-04-21" cidade="Sintra" nrutente="177218543">          <COVID>Não</COVID>
</Isolado>
        <Isolado nome="António Costa " datanasc="1970-02-17" cidade="Lisboa" nrutente="1945233872">          <COVID>Sim</COVID>
</Isolado>
        <Isolado nome="Paulo Portas" datanasc="1966-08-09" cidade="Setubal" nrutente="123210982">          <COVID>Não</COVID>
</Isolado>
        <Isolado nome="Jeronimo Martins" datanasc="1989-02-29" cidade="Évora" nrutente="178218972">        <COVID>Sim</COVID>
</Isolado>
        <Isolado nome="José Socrates" datanasc="1989-02-29" cidade="Covilhã" nrutente="156234872">          <COVID>Não</COVID>
</Isolado>
	
</Contagiados>


What I have tried:

I have try to use dataset like this for example:
Iservice1.svc

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetName(int rno);

      
    }    


and my Service1.cs

public class Service1 : IService1
    {
 

        public string GetName(int rno)
        {
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(@"C:\Users\Diogo Rocha\Desktop\WcfService1\WcfService1\XMLInfetedFile.xml");
                string res = ds.Tables[0].Rows[rno][0].ToString();
                return res;
            }
            catch
            {
                return "No More Data";
            }
        }
    }
Posted
Updated 12-Nov-21 3:11am

1 solution

Use an XDocument. There is also the older XmlDocument. Both will work, but be careful not mixing them up when googling.

This page contains an example showing how to load an XDocument:
XDocument.Load Method (System.Xml.Linq) | Microsoft Docs[^]

Quick googling led me to this page that looks reasonable at first sight:
https://home.csulb.edu/~pnguyen/cecs475/pdf/linqtoxmlnew.pdf

I would only use DataSets when dealing with relational data.... and then only if there is no way to use a strongly typed object model instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900