I'm calling a "web service" that gives me as an xml response an invalid node, so when I try to deserialize it, it throws an exception.
I'm using the XmlSerializer class, like so:
internal class Response<T>
{
public Response(byte[] xml)
{
XmlSerializer s = new XmlSerializer(typeof(T));
XmlReader reader = XmlReader.Create(new MemoryStream(xml));
if (s.CanDeserialize(reader))
this.ActualResponse = (T)s.Deserialize(reader);
}
public T ActualResponse { get; private set; }
}
and the node I'm having trouble with looks something like this:
<autorizacion>FALSE</autorizacion>
The exception I get is
System.InvalidOperationException:
There is an error in XML document (7,
35). ---> System.FormatException: The
string 'FALSE' is not a valid Boolean
value..
Which is obvious.
The question is, how can I deserialize it without having to iterate through all nodes, building my response entity by hand? Is there a way?
I don't have control over the server
Quickest way seems to be to change the parameter of setAutorizacion(boolean) to setAutorizacion(String), then convert to a boolean in the setter. Also, document what you did and why you did it both in that setter and in more high-level documentation.
you can use an Xsl to reformat your xml before the deserialization
EDIT
for xsl transform with c#: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
your xsl should contains something like that
<xsl:template match="autorizacion">
<autorizacion><xsl:value-of select="concat(upper-case(substring(current(),1,1)), substring(current(),2))" />
</autorizacion>
</xsl:template>
you'll have to format it without validating it to your schema. format the values that are wrong and revalidate it with schema.
Related
I have an object that I serialize into an XML file. Everything works as expected, but I'm wanting to add a Version attribute to the root element.
What's the best way to do this?
Here's an example of how I'm serializing:
MyProgram newProgram = new MyProgram()
{
ValueA = "A value.",
ValueB = "B value.",
ValueC = "C value."
};
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyProgram));
StreamWriter streamWriter = new StreamWriter(fileName);
xmlSerializer.Serialize(streamWriter, newProgram);
streamWriter.Close();
Right now, my XML looks something like this:
<MyProgram>
<ValueA>A value.</ValueA>
<ValueB>B value.</ValueB>
<ValueC>C value.</ValueC>
</MyProgram>
But I'd like to have this:
<MyProgram Version="1.0">
<ValueA>A value.</ValueA>
<ValueB>B value.</ValueB>
<ValueC>C value.</ValueC>
</MyProgram>
Thanks!
First
Modify your XML to be like this, assuming you have a concrete class and not an anonymous one
[XmlRoot("MyProgram")]//sepcifies the name of the root element
public class MyProgram
{
[XmlAttribute("Version")]//name not required unless you want to change output to something different
public string Version{get;set;}
[XmlElement("ValueA")]//again, name not required if the name is the same
public ValueA ValueA{get;set;}
....
}
Then creating the MyProgram class as you specified will give you the desired output, also note that you might need to add XML tags to the ValueA/B/C classes if the end result is not as desired.
The second
Serialize the XML data to a string, and use simple Regex/String manipulation to insert the desired value and then save the string to the desired location
Third
You can either use XElement to query your XML string /Create it and then set the Version attribute
XElement x = XElement.Load("Your XML location");
var yourRoot = x.Descendants("MyProgram").FirstOrDefaul();
yourRoot.SetAttributeValue("Version","1.0");
yourRoot.Save("Your XML location");
I am trying to generate an XML file from an Object. I put a break point before generating the XML file, so I can check the values. The object and its value look fine. However, after the XML file is generated, it is missing a key component, the code.
This is what I was expected to see.
<eDocument Code="UN" Cat="ST">
<id myId="5"/>
</eDocument>
This is the actual xml file that is generated.
<eDocument Cat="EST">
<id myId="5"/>
</eDocument>
This is the object that is being serialize to generate the xml file.
sDoc eDocument = new sDoc();
eDocument.Code = "UN";
eDocument.Cat = "ST";
eDocument.myId = new ID[1];
eDocument.myId[0].id= 5;
This is how I am saving the file
string fileName= "student.xml";
XmlSerializer serializeObject = new XmlSerializer(eDocument.GetType());
TextWriter streamWritter = new StreamWriter(Server.MapPath(#"~/student/" + fileName));
serializeObject.Serialize(streamWritter, eDocument); // I check the eDocument Object, and it has all the correct inforamtion
streamWritter.Close();
Is there something that I am doing wrong here?
You need to check the "Code" property in the sDoc class.
A property should be public read/write in order to be Xml-serializable. By default, if no attribute is applied to a public property, it is serialized as an XML element. In your case it's not serialized at all, which means something is wrong.
First check: is the property public in both read (get) and write (set)?
Second check: Isn't the field marked with [XmlIgnoreAttribute]?
And finally: Marking the Cat property with [XmlAttribute] will xml-serialize it as an attribute.
When i am doing Deserialize of xml i am getting "There is an error in XML document (1, 41)." . Can anyone tell me about what is the issue is all about.
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
I use this function to do it.
<?xml version='1.0' encoding='utf-16'?>
<Message>
<FirstName>Hunt</FirstName>
<LastName>DAvid</LastName>
</Message>
Ensure your Message class looks like below:
[Serializable, XmlRoot("Message")]
public class Message
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This works for me fine:
string xml = File.ReadAllText("c:\\Message.xml");
var result = DeserializeFromXml<Message>(xml);
MSDN, XmlRoot.ElementName:
The name of the XML root element that is generated and recognized in
an XML-document instance. The default is the name of the serialized
class.
So it might be your class name is not Message and this is why deserializer was not able find it using default behaviour.
Agreed with the answer from sll, but experienced another hurdle which was having specified a namespace in the attributes, when receiving the return xml that namespace wasn't included and thus failed finding the class.
i had to find a workaround to specifying the namespace in the attribute and it worked.
ie.
[Serializable()]
[XmlRoot("Patient", Namespace = "http://www.xxxx.org/TargetNamespace")]
public class Patient
generated
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxxx.org/TargetNamespace">
but I had to change it to
[Serializable()]
[XmlRoot("Patient")]
public class Patient
which generated to
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
This solved my problem, hope it helps someone else.
First check the variables declared using proper Datatypes.
I had a same problem then I have checked, by mistake I declared SAPUser as int datatype so that the error occurred.
One more thing XML file stores its data using concept like array but its first index starts having +1.
e.g. if error is in(7,2) then
check for 6th line always.....
I had the same thing. All came down to a "d" instead of a "D" in a tag name in the schema.
In my case I had a float value expected where xml had a null value so be sure to search for float and int data type in your xsd map
On a WEC7 project I'm working on, I got a similar error. The file I was serializing in was serialized out from an array of objects, so I figured the XML was fine. Also, I have had this working for a few previous classes, so it was quite a puzzle.
Then I noticed in my earlier work that every class that I was serializing/deserializing had a default constructor. That was missing in my failed case so I added it and and voila... it worked fine.
I seem to remember reading somewhere that this was required. I guess it is.
I've been trying to pull the value of the XML node into a string. Here is what the XML looks like:
<currentvin value="1FTWW31R08EB18119" />
I can't seem to figure out how to grab that value. I didn't write this XML, by the way. So far I have tried several approaches, including the following:
public void xmlParse(string filePath)
{
XmlDocument xml = new XmlDocument();
xml.Load(filePath);
XmlNode currentVin = xml.SelectSingleNode("/currentvin");
string xmlVin = currentVin.Value;
Console.WriteLine(xmlVin);
}
Which doesn't work. I then tried:
public void xmlParse(string filePath)
{
XmlDocument xml = new XmlDocument();
xml.Load(filePath);
string xmlVin = xml.SelectSingleNode("/currentvin").Value;
Console.WriteLine(xmlVin);
}
But that doesn't work either. I am getting a null reference exception stating that Object reference not set to an instance of an object. Any ideas?
I think you're confusing the Value property of the XmlNode class, with an XML attribute named "value".
value is an attribute in your xml so either modify your xpath query to be
xml.SelectSingleNode("/currentvin/#value").Value
Or user the Attributes collection of the selected XmlNode.
You are looking for the value of the attribute "value" (that's a handful) not the value of the node itself - so you have to use the Attribute property:
string xmlVin = xml.SelectSingleNode("/currentvin").Attributes["value"].Value;
Or in the first version:
XmlNode currentVin = xml.SelectSingleNode("/currentvin");
string xmlVin = currentVin.Attributes["value"].Value;
If your entire XML contains only this node then it could be xml.DocumentElement.Attributes["value"].Value;
I am creating an application which requires to convert c# object to XML.
I am using XML Serializer class to achieve this. Here is the code snippet:
public class Anwer
{
public int ID { get; set; }
public string XML { get; set; }
public Anwer(int ID, string XML)
{
this.ID = ID;
this.XML = XML;
}
public Anwer() { }
}
Here is the main function:
string AnswerXML = #"<Answer>1<Answer>";
List<Anwer> answerList = new List<Anwer>();
answerList.Add(new Anwer(1,AnswerXML));
AnswerXML = #"<Answer>2<Answer>";
answerList.Add(new Anwer(2, AnswerXML));
XmlSerializer x = new XmlSerializer(answerList.GetType());
x.Serialize(Console.Out, answerList);
The output is:
<?xml version="1.0" encoding="IBM437"?>
<ArrayOfAnwer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
ttp://www.w3.org/2001/XMLSchema">
<Anwer>
<ID>1</ID>
<XML><Answer>1<Answer></XML>
</Anwer>
<Anwer>
<ID>2</ID>
<XML><Answer>2<Answer></XML>
</Anwer>
</ArrayOfAnwer>
In the above code '<' and '>' are getting replaced by '<' and '>';
How to avoid this?
I know string replace is one of the way, but I don't want to use it.
Thanks in advance.
You don't, basically. That's correctly serializing the object - the XML serializer doesn't want to have to deal with XML within strings messing things up, so it escapes the XML appropriately.
If you deserialize the XML later, you'll get back to the original object data.
If you're trying to build up an XML document in a custom fashion, I suggest you don't use XML serialization to start with. Either use LINQ to XML if you're happy to create elements etc explicitly, or if you really, really want to include arbitrary strings directly in your output, use XmlWriter.
If you could give us more information about the bigger picture of what you're trying to do, we may be able to suggest better alternatives - building XML strings directly is almost never a good idea.
XmlSerializer won't believe you that an element is xml unless you convince it, for example by exposing that property as an XmlDocument. Otherwise, it (correctly, IMO) always encodes such values. For example:
using System;
using System.Xml;
using System.Xml.Serialization;
public class Anwer
{
public int ID { get; set; }
public XmlDocument XML { get; set; }
public Anwer(int ID, string XML)
{
this.ID = ID;
XmlDocument doc = new XmlDocument();
doc.LoadXml(XML);
this.XML = doc;
}
public Anwer()
{ }
}
static class Program
{
static void Main()
{
var answer = new Anwer(123, "<Answer>2</Answer>");
var ser = new XmlSerializer(answer.GetType());
ser.Serialize(Console.Out, answer);
}
}
I am creating an application which requires to convert c# object to XML. I am using XML Serializer class to achieve this
If you're using the XML Serializer to do the work, then why the "XML" field where you're inserting hand-coded XML? Seems like you want something more like this (using your class name, though it looks like a misspelling):
public class Anwer
{
public int ID { get; set; }
public int Answer { get; set; }
}
..
List<Anwer> answerList = new List<Anwer>() {
new Anwer { ID=1, Answer=2 },
new Anwer { ID=2, Answer=3 },
};
XmlSerializer x = new XmlSerializer(answerList.GetType());
x.Serialize(Console.Out, answerList);
..
<ArrayOfAnwer ...>
<Anwer>
<ID>1</ID>
<Answer>2</Answer>
</Anwer>
...
Or if you actually want/need the Answer element to be nested in an XML element for some reason, you can alter your Anwer object to reflect that structure (as Oleg Kalenchuk suggests), or generate the XML yourself rather than using the serializer:
XElement xml = new XElement("AnwerList",
from anwer in anwerList select
new XElement("Anwer",
new XElement("ID", anwer.ID),
new XElement("XML",
new XElement("Answer", anwer.Answer)
)
)
);
Console.Out.WriteLine(xml);
<AnwerList>
<Anwer>
<ID>1</ID>
<XML>
<Answer>2</Answer>
</XML>
</Anwer>
...
I prefer the latter anyway, because it gives you more control.
You're assigning a string containing the < and > sign to the XML element so it is obvious that teh serializer would replace the < and > with entity references. Even if you're getting > in the text when you deserialise the XML you'll get the > in your text.
Create a new class AnswerXML with one integer "Answer" member
Change type of XML member to AnswerXML instead of string
Because '<' and '>' are characters used for the xml-structure itself, they are automatically htmlencoded. When you read it back in your app and deserialize it, the '<' and '>' should be converted back to '<' and '>'.
If your goal is otherwise, use htmldecode functionality.
If this don't help, just tell what exactly you want to do with the xml-data.