In this article just see how to get SQL Server Table Records in XML format. Most developers just want to get there record in XML format so that they can easily use this data in Web Services or REST full services.
Firstly, open your SQL Server and create a table with any script or schema or you can copy the following script:
- CREATE TABLE [dbo].[Employee](
- [Id] [int] NOT NULL,
- [Name] [varchar](50) NULL,
- [Salary] [int] NULL,
- [DptId] [int] NULL);
Now insert some records for test query as in the following query:
- INSERT INTO [dbo].[Employee]
- ([Id]
- ,[Name]
- ,[Salary]
- ,[DptId])
- VALUES
- (<Id, int,>
- ,<Name, varchar(50),>
- ,<Salary, int,>
- ,<DptId, int,>)
- GO
Or you can copy the following queries:
Insert into Employee values.
- (101,'Nitin Pandit',12345,111),
- (102,'Rahul Pandit',12345,222),
- (103,'Mohit Pandit',12345,333),
- (104,'Sonu Pandit',12345,444)
Now just run the select command to get the records.
Your output must look like the following image.
Now if you want to get the data in XML so just write your query as in the following:
FOR XML path, root;
When you run this query so you’ll get your records in XMl and if you want to see all data just click on the link:
This is your data from SQL Server table to XML Records.
- <root>
- <row>
- <Id>101</Id>
- <Name>Nitin Pandit</Name>
- <Salary>12345</Salary>
- <DptId>111</DptId>
- </row>
- <row>
- <Id>102</Id>
- <Name>Rahul Pandit</Name>
- <Salary>12345</Salary>
- <DptId>222</DptId>
- </row>
- <row>
- <Id>103</Id>
- <Name>Mohit Pandit</Name>
- <Salary>12345</Salary>
- <DptId>333</DptId>
- </row>
- <row>
- <Id>104</Id>
- <Name>Sonu Pandit</Name>
- <Salary>12345</Salary>
- <DptId>444</DptId>
- </row>
- </root>
Thanks for reading this article. Share it and if you have any questions, then please mention in the comments section.
Connect (“Nitin Pandit”);