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>
c# xml asp.net-web-api nested
add a comment |
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>
c# xml asp.net-web-api nested
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
add a comment |
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>
c# xml asp.net-web-api nested
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
c# xml asp.net-web-api nested
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
add a comment |
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
add a comment |
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!")
})
);
}
}
}
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
add a comment |
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!")
})
);
}
}
}
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
add a comment |
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!")
})
);
}
}
}
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
add a comment |
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!")
})
);
}
}
}
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!")
})
);
}
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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