How can I create a webapi with nested xml from ssms table?











up vote
0
down vote

favorite












I have made a web api with C# and an ssms table. The result is an xml with only elements.



//This is only an example


<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


How can I format my xml as nested like this and add an optional different header from ssms table element??:



<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<example>
<from>Jani
<heading>Reminder</heading>
</from>
<body>Don't forget me this weekend!</body>
</example>
</note>









share|improve this question






















  • And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
    – Freek W.
    Nov 8 at 9:58










  • @FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
    – chrysa22
    Nov 8 at 10:01










  • <from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
    – Freek W.
    Nov 8 at 10:43










  • @Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
    – chrysa22
    Nov 8 at 11:07















up vote
0
down vote

favorite












I have made a web api with C# and an ssms table. The result is an xml with only elements.



//This is only an example


<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


How can I format my xml as nested like this and add an optional different header from ssms table element??:



<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<example>
<from>Jani
<heading>Reminder</heading>
</from>
<body>Don't forget me this weekend!</body>
</example>
</note>









share|improve this question






















  • And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
    – Freek W.
    Nov 8 at 9:58










  • @FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
    – chrysa22
    Nov 8 at 10:01










  • <from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
    – Freek W.
    Nov 8 at 10:43










  • @Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
    – chrysa22
    Nov 8 at 11:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have made a web api with C# and an ssms table. The result is an xml with only elements.



//This is only an example


<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


How can I format my xml as nested like this and add an optional different header from ssms table element??:



<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<example>
<from>Jani
<heading>Reminder</heading>
</from>
<body>Don't forget me this weekend!</body>
</example>
</note>









share|improve this question













I have made a web api with C# and an ssms table. The result is an xml with only elements.



//This is only an example


<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


How can I format my xml as nested like this and add an optional different header from ssms table element??:



<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<example>
<from>Jani
<heading>Reminder</heading>
</from>
<body>Don't forget me this weekend!</body>
</example>
</note>






c# xml asp.net-web-api nested






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 9:27









chrysa22

12




12












  • And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
    – Freek W.
    Nov 8 at 9:58










  • @FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
    – chrysa22
    Nov 8 at 10:01










  • <from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
    – Freek W.
    Nov 8 at 10:43










  • @Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
    – chrysa22
    Nov 8 at 11:07


















  • And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
    – Freek W.
    Nov 8 at 9:58










  • @FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
    – chrysa22
    Nov 8 at 10:01










  • <from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
    – Freek W.
    Nov 8 at 10:43










  • @Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
    – chrysa22
    Nov 8 at 11:07
















And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
– Freek W.
Nov 8 at 9:58




And why exactly is it that you want such a format? It seems you can manually add the heading tag withing your <from /> field, but what are you trying to achieve by doing this?
– Freek W.
Nov 8 at 9:58












@FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
– chrysa22
Nov 8 at 10:01




@FreekW. I'm afraid it's a standard and I should do it like this automatically with c#..:)
– chrysa22
Nov 8 at 10:01












<from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
– Freek W.
Nov 8 at 10:43




<from>Jani<heading>Reminder</heading></from> Like this? Because I just create datacontracts and serialize those classes and they fill them like this <from><heading>Reminder</heading></from> Because data should be added in variable not the collection.
– Freek W.
Nov 8 at 10:43












@Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
– chrysa22
Nov 8 at 11:07




@Freek W. like this yeah, this was only an example..I wanted to have a way, not a ready solution!
– chrysa22
Nov 8 at 11:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Use xml linq :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string xml =
"<?xml version="1.0" encoding="UTF-8"?>" +
"<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>";

XDocument doc = XDocument.Parse(xml);

XElement from = doc.Descendants("from").FirstOrDefault();
XElement heading = doc.Descendants("heading").FirstOrDefault();
XElement body = doc.Descendants("body").FirstOrDefault();

from.Add(heading);

XElement example = new XElement("example", new object {from,body});
heading.Remove();
body.Remove();
from.ReplaceWith(example);


}
}
}




If you are creating from scratch



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string ident = "<?xml version="1.0" encoding="UTF-8"?><note></note>";

XDocument doc = XDocument.Parse(ident);
XElement note = doc.Root;

note.Add(new XElement("to", "Tove"),
new XElement("example", new object {
new XElement("from", new object {
"Jani", new XElement("heading", "Reminder")
}),
new XElement("body","Don't forget me this weekend!")
})
);

}
}
}





share|improve this answer























  • thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
    – chrysa22
    Nov 8 at 11:04












  • I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
    – jdweng
    Nov 8 at 14:16













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204793%2fhow-can-i-create-a-webapi-with-nested-xml-from-ssms-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Use xml linq :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string xml =
"<?xml version="1.0" encoding="UTF-8"?>" +
"<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>";

XDocument doc = XDocument.Parse(xml);

XElement from = doc.Descendants("from").FirstOrDefault();
XElement heading = doc.Descendants("heading").FirstOrDefault();
XElement body = doc.Descendants("body").FirstOrDefault();

from.Add(heading);

XElement example = new XElement("example", new object {from,body});
heading.Remove();
body.Remove();
from.ReplaceWith(example);


}
}
}




If you are creating from scratch



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string ident = "<?xml version="1.0" encoding="UTF-8"?><note></note>";

XDocument doc = XDocument.Parse(ident);
XElement note = doc.Root;

note.Add(new XElement("to", "Tove"),
new XElement("example", new object {
new XElement("from", new object {
"Jani", new XElement("heading", "Reminder")
}),
new XElement("body","Don't forget me this weekend!")
})
);

}
}
}





share|improve this answer























  • thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
    – chrysa22
    Nov 8 at 11:04












  • I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
    – jdweng
    Nov 8 at 14:16

















up vote
0
down vote













Use xml linq :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string xml =
"<?xml version="1.0" encoding="UTF-8"?>" +
"<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>";

XDocument doc = XDocument.Parse(xml);

XElement from = doc.Descendants("from").FirstOrDefault();
XElement heading = doc.Descendants("heading").FirstOrDefault();
XElement body = doc.Descendants("body").FirstOrDefault();

from.Add(heading);

XElement example = new XElement("example", new object {from,body});
heading.Remove();
body.Remove();
from.ReplaceWith(example);


}
}
}




If you are creating from scratch



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string ident = "<?xml version="1.0" encoding="UTF-8"?><note></note>";

XDocument doc = XDocument.Parse(ident);
XElement note = doc.Root;

note.Add(new XElement("to", "Tove"),
new XElement("example", new object {
new XElement("from", new object {
"Jani", new XElement("heading", "Reminder")
}),
new XElement("body","Don't forget me this weekend!")
})
);

}
}
}





share|improve this answer























  • thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
    – chrysa22
    Nov 8 at 11:04












  • I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
    – jdweng
    Nov 8 at 14:16















up vote
0
down vote










up vote
0
down vote









Use xml linq :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string xml =
"<?xml version="1.0" encoding="UTF-8"?>" +
"<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>";

XDocument doc = XDocument.Parse(xml);

XElement from = doc.Descendants("from").FirstOrDefault();
XElement heading = doc.Descendants("heading").FirstOrDefault();
XElement body = doc.Descendants("body").FirstOrDefault();

from.Add(heading);

XElement example = new XElement("example", new object {from,body});
heading.Remove();
body.Remove();
from.ReplaceWith(example);


}
}
}




If you are creating from scratch



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string ident = "<?xml version="1.0" encoding="UTF-8"?><note></note>";

XDocument doc = XDocument.Parse(ident);
XElement note = doc.Root;

note.Add(new XElement("to", "Tove"),
new XElement("example", new object {
new XElement("from", new object {
"Jani", new XElement("heading", "Reminder")
}),
new XElement("body","Don't forget me this weekend!")
})
);

}
}
}





share|improve this answer














Use xml linq :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string xml =
"<?xml version="1.0" encoding="UTF-8"?>" +
"<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>";

XDocument doc = XDocument.Parse(xml);

XElement from = doc.Descendants("from").FirstOrDefault();
XElement heading = doc.Descendants("heading").FirstOrDefault();
XElement body = doc.Descendants("body").FirstOrDefault();

from.Add(heading);

XElement example = new XElement("example", new object {from,body});
heading.Remove();
body.Remove();
from.ReplaceWith(example);


}
}
}




If you are creating from scratch



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication78
{
class Program
{

static void Main(string args)
{

string ident = "<?xml version="1.0" encoding="UTF-8"?><note></note>";

XDocument doc = XDocument.Parse(ident);
XElement note = doc.Root;

note.Add(new XElement("to", "Tove"),
new XElement("example", new object {
new XElement("from", new object {
"Jani", new XElement("heading", "Reminder")
}),
new XElement("body","Don't forget me this weekend!")
})
);

}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 14:15

























answered Nov 8 at 10:52









jdweng

16.5k2717




16.5k2717












  • thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
    – chrysa22
    Nov 8 at 11:04












  • I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
    – jdweng
    Nov 8 at 14:16




















  • thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
    – chrysa22
    Nov 8 at 11:04












  • I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
    – jdweng
    Nov 8 at 14:16


















thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
– chrysa22
Nov 8 at 11:04






thank you very very much...I don't want to do this for every single xml and parse it! But I want it to be produced in a specific way, from different ssms table elements!
– chrysa22
Nov 8 at 11:04














I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
– jdweng
Nov 8 at 14:16






I update code to show how to generate from scratch. You can use the SQLClient DataAdapter method to create a DataTable. The use the DataTable WriteXml() method to same DataTable to a file.
– jdweng
Nov 8 at 14:16




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204793%2fhow-can-i-create-a-webapi-with-nested-xml-from-ssms-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()