Using Refit, how to debug deserialization error? - c#

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
{
}
}

Related

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

How to get the version number from a given XSLT file .

Say I have an XSLT file like below:
`<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-
prefixes="xs math"
version="3.0">`
....... and so on.
I need the output as 3.0 because the above file has version="3.0". I want to use C# to get this given the XSLT is in string format
Using xml linq :
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);
string version = (string)doc.Root.Attribute("version");
}
}
}
Use XElement.Parse(yourString).Attribute("version").Value, where you add using System.Xml.Linq;. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview for details of the used API.

XML file to other schema based XML file using c#

is it possible to rewrite XML file according to other schema XSD using c# ?
this is XML file
this is the current schema XSD file
and this is the new schema same everything but changed node names
so how to get a new XML from the old one based on the new XSD using c#?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication13
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> shipTo = doc.Descendants("shipto").ToList();
foreach (XElement ship in shipTo)
{
ship.Element("name").ReplaceWith(new XElement("FullName", (string)ship.Element("name")));
ship.Element("address").ReplaceWith(new XElement("FirstAddress", (string)ship.Element("address")));
ship.Element("city").ReplaceWith(new XElement("homeTown", (string)ship.Element("city")));
ship.Element("country").ReplaceWith(new XElement("HomeLand", (string)ship.Element("country")));
}
}
}
}

Add Node at begin and end of the existing xml file using c#

I generated a XML file through API call then I tried to read the file using XML source component in ssis but it is read only data sets except all data contains in file .
Here my file
<?XML version 1.0 >
<ABC>
<a>info<a/>
<ABC/>
But I want file like below then only I can easily read file using component
We can manipulate the file manually for single file but not for thousand files
<?XML Version 1.0>
<X>
<ABC>
<a>info <a/>
<ABC/>
</X>
How to add that 'X' node to the existing file .
I am not having much exposure on .Net technology .
Kindly help me at the earliest of time .
Thank You
KiranKumar
Using xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<ABC>" +
"<a>info</a>" +
"</ABC>";
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
root.ReplaceWith(new XElement("X", root));
}
}
}
Try streaming API.
using (var reader = XmlReader.Create("test.xml"))
using (var writer = XmlWriter.Create("test2.xml"))
{
writer.WriteStartElement("X");
reader.MoveToContent();
writer.WriteNode(reader.ReadSubtree(), true);
writer.WriteEndElement();
}
This approach handles xml without excessive memory consumption.
Also, this method allows to modify xml on the fly, getting it from the input API stream and writing to output stream.
using (var reader = XmlReader.Create(inputStream))
using (var writer = XmlWriter.Create(outputStream))

Select InnerText of XML is decoding URL

My issue is, when selecting a node from xml file (packet in my case) is decoding an url that is the inner text of the node within the xml. Example
<url>"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2"</url>
using an instance of XmlDocument:
xmlDoc.SelectSingleNode("url").InnerText;
The string that is returned is: "http://my.xml.org/?id=AAA/DDDD/NNNLKLKJLKL/"
How do I prevent this from happening? Notice that the %2 and %3's have been changed to forward slashes.
The code below gets correct results. What are you doing differently?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<url>\"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2\"</url>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string results = xmlDoc.SelectSingleNode("url").InnerText;
}
}
}
​

Categories