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

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>

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!")
})
);
}
}
}

Related

Using Refit, how to debug deserialization error?

I'm using Refit to consume an XML ReST Web Api. There is some issue with XML deserialization into POCOs (which are annotated with attributes from System.Xml.Serialization) since I get a NullReferenceException in Refit.RequestBuilderImplementation.DeserializeContentAsync.
However, I a no further information on how to debug the issue, since the exception message tells me nothing on what attribute or object in the response is not adapted to my POCOs.
The XMl is :
<?xml version="1.0" encoding="utf-16"?>
<MediaContainer friendlyName="myPlex" identifier="com.plexapp.plugins.myplex" machineIdentifier="c62be90a37d43ecd9596be8abd98a2e485979e31" size="2">
<Server accessToken="plexauthtoken" name="VOSTOK" address="ip address" port="1" version="1.23.0.4497-a1b1f3c10" scheme="http" host="ip address" localAddresses="ipaddress1,ipaddress2" machineIdentifier="machineid" createdAt="1590584258" updatedAt="1621708154" owned="1" synced="0" />
</MediaContainer>
Net library doesn't like "utf-16". Solution is to skip first line of xml like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<MediaContainer>\n</MediaContainer>";
StringReader sReader = new StringReader(xml);
sReader.ReadLine();
XmlReader xReader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(MediaContainer));
MediaContainer MediaContainer = (MediaContainer)serializer.Deserialize(xReader);
}
}
public class MediaContainer
{
}
}

Altering string variable storing xml in c#

string content = ....
I have some XML content stored in a string as shown in the variable above. The content stored is similar to below ,
<?xml version="1.0"?>
<model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<ItemModel xsi:type="TypeKeep">
<Name>Name01</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name02</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name03</Name>
</ItemModel>
</Items>
</model>
In here I want to remove all the elements which have the type="TypeDelete" . In the sense I am trying to alter the content variable by removing the elments of TypeDelete
Any idea how I can achieve this ?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XNamespace xsiNs = doc.Root.GetNamespaceOfPrefix("xsi");
List<XElement> removeNodes = doc.Descendants("ItemModel").Where(x => (string)x.Attribute(xsiNs + "type") == "TypeDelete").ToList();
for (int i = removeNodes.Count - 1; i >= 0; i--)
{
removeNodes[i].Remove();
}
}
}
}
If you want to remove a specific XML node from an XML file directly you can go through this link and try this

Modifying two fields in .XML file

I have a XML file which looks like:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<UFCC xmlns="http://actel.com/sweng/afi">
<Region name="Region_0_0">
<content>
<static_data>
<fixed>
<value>11111111111111111111111111111111</value>
<type>HEX</type>
</fixed>
</static_data>
</content>
<region_start_word>0</region_start_word>
<number_of_word>16</number_of_word>
<simulation_value>
<value>11111111111111111111111111111111</value>
<type>HEX</type>
</simulation_value>
</Region>
<Region name="Region_1_0">
<content>
<static_data>
<fixed>
<value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
<type>HEX</type>
</fixed>
</static_data>
</content>
<region_start_word>16</region_start_word>
<number_of_word>16</number_of_word>
<simulation_value>
<value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
<type>HEX</type>
</simulation_value>
</Region>
<Program_Pages/>
</UFCC>
I'm trying to change value fields "11111111111111111111111111111111" to "A...A". I have tried this with Xml.Linq and using examples found online, from which none seem to work.
How can I modify these fields?
Sorry about the formatting, I'm not that familiar with proper XML formatting and the source file is written in single line.
You need to use the namespace :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
List<XElement> values1s = doc.Descendants(ns + "value").Where(x => (string)x == "11111111111111111111111111111111").ToList();
foreach (XElement x in values1s)
{
x.SetValue(((string)x).Replace("1", "A"));
}
}
}
}

AvalonEdit - XMLFolding -> Ignore the first line

Would it be possible to ignore the first line when trying to update the foldings?
Ignore this line
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</Ffrom>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Right now this is not recognized as a valid XMLDocument and the reader will throw an exception.
Following will skip first line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
reader.ReadLine(); // skip first line
XDocument doc = XDocument.Load(reader);
}
}
}

Read from 2 XML files using StreamReader

Assuming I have the following XML files:
Sample1.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<NodeA>
<Node1>test1</Node1>
<Node1>test2</Node1>
</NodeA>
</Root>
Sample2.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<NodeB>
<Node1>test1</Node1>
<Node1>test2</Node1>
</NodeB>
</Root>
My goal is to deserialize those 2 xml files using one factory method.
For that I have the following generic code:
public static T get<T>() where T : class // class = all reference types
{
string Ttype = typeof(T).Name;
using (var streamReader1 = new StreamReader(directoryPath + #"\Sample1.xml"))
using (var streamReader2 = new StreamReader(directoryPath + #"\Sample2.xml"))
using (var xmlReader ??? = XmlReader.Create(streamReader))
{
...
}
}
Built both xml files with the same root element.
My question is, since the xmlReader create function accepts only 1 stream reader.
How in the name of *, do i read with the xml reader from 2 xmls?
Do I combine 2 streams into 1 and read from 1st bit to the last 1, if so, how?
Thanks in advance,
Montana.
I would do it like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME1 = #"c:\temp\test.xml";
const string FILENAME2 = #"c:\temp\test2.xml";
static void Main(string[] args)
{
XDocument doc1 = XDocument.Load(FILENAME1);
XElement root1 = (XElement)doc1.FirstNode;
XDocument doc2 = XDocument.Load(FILENAME2);
XElement root2 = (XElement)doc2.FirstNode;
root1.Add(root2.Elements());
}
}
}

Categories