I have this code to generate Xml from an IQueryable variable query:
XDocument data = XDocument.Load(HttpContext.Current.Server
.MapPath("~/XMLFile.xml"));
XElement newTest = new XElement("TestForPackage",
from Tests in query.AsEnumerable()
select new XElement("Tests",
new XElement("Dep_Code", Tests.Dep_Code),
new XElement("Dep_Name", Tests.Dep_Name),
new XElement("Test_Code", Tests.Test_Code),
new XElement("Test_Name", Tests.Test_Name),
new XElement("Sub_Test_Code", Tests.Sub_Test_Code),
new XElement("Sub_Test_Code", Tests.Sub_Test_Name)
));
data.Element("TestsForPackage").Add(newTest);
data.Save(HttpContext.Current.Server.MapPath("~/XMLFile.xml"));
It generates xml in the format below:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<TestsForPackage>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
</TestsForPackage>
However, I need to generate xml file in this format:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<TestsForPackage>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
<Test>
<Dep_Code>BIOCH</Dep_Code>
<Dep_Name>BIOCHEMISTRY</Dep_Name>
<Test_Code>BGPP</Test_Code>
<Test_Name>BLOOD GLUCOSE P.P.</Test_Name>
<Sub_Test_Code />
<Sub_Test_Name />
</Test>
</TestsForPackage>
I know I am making some minor mistake but I don’t know what it is.
XML is all about semantics, it is a mechanism for transmission and exchange of data. An XML parser doesn’t care about the whitespace in your file. For this reason, XML writers do not allow you to control the whitespace output (other than giving your the option to pretty-print).
If you want to be specific about where the line-breaks are in your XML output, you are going to have to manipulate the XML output yourself using string manipulation.