I'm currently working with generating XML files used to configure a server.
I've got a class generated with xsd which contains a property of the type System.Xml.XmlElement.
public class GeneratedClass
{
...
private System.Xml.XmlElement informationField;
[System.Xml.Serialization.XmlArrayItemAttribute("Information", IsNullable=false)]
public System.Xml.XmlElement Information {
get {
return this.informationField;
}
set {
this.informationField = value;
}
}
...
}
I'm having troubles "injecting" a custom object into this Information property.
public class MyExampleObject
{
public string Name { get; set; }
public string Id { get; set;
}
The program deserializes a xml file of the type GeneratedClass and after this I want to add MyExampleObject to the Informations property.
The way I currently do this is with this method:
XmlDocument doc = new XmlDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
{
XmlSerializer serializer = new XmlSerializer(typeof(MyExampleObject));
serializer.Serialize(writer, MyObject);
}
this.Information = doc.DocumentElement;
After this I serialize the whole object to file, but when I do this I get unwanted xml namespace attributes.
<Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">
I've found other posts with similar problems, but the suggested solutions, leaves me with <Information xmlns=""> which is still not a complete solution.
I get the feeling that there might be some other way to do this, but I'm not sure how.
Any suggestions?
I know that this post is very old :) But perhaps might come in handy for others seeking answer to this
Update Sorry I was a bit quick reading your question and focus on my own problem which was also regarding namespaces in some sense, but more how to remove one. I see that you use XmlSerializer and unfortunately I don't know how to manipulate namespaces added as attribute on class to serialize. Perhaps look at XmlRootAttribute that you can pass to XmlSerializer.ctor and then you have XmlSerializerNamespaces which is passed during serialization and provide mean to manipulate namespaces.
I recently stumbled upon this as well and found a solution.
var xmlDocument = new XmlDocument();
var xmlElement = xmlDocument.CreateElement(Prefix, "Document", Ns);
xmlElement.InnerXml = string.Empty;
var navigator = xmlElement.CreateNavigator();
navigator.AppendChildElement(Prefix, "InfRspnSD1", Ns, null);
navigator.MoveToFirstChild();
navigator.AppendChildElement(Prefix, "InvstgtnId", Ns, "123456789");
// At this point xmlElement contains XML and can be assigned
// to the generated class
// For pretty print only
using var sw = new StringWriter();
using var writer = System.Xml.XmlWriter.Create(sw, Settings);
xmlElement.WriteTo(writer);
writer.Flush();
return sw.ToString();
(Code on GitHub)
What I've noticed is that the namespace is included twice, one in the root node and then once again immediate descendant (first child).
# Generate XML using XNavigator
<?xml version="1.0" encoding="utf-16"?>
<supl:Document xmlns:supl="urn:iso:std:iso:20022:tech:xsd:supl.027.001.01">
<supl:InfRspnSD1 xmlns:supl="urn:iso:std:iso:20022:tech:xsd:supl.027.001.01">
<supl:InvstgtnId>123456789</supl:InvstgtnId>
</supl:InfRspnSD1>
</supl:Document>
--------------------
Related
Given this generic serialization code:
public virtual string Serialize(System.Text.Encoding encoding)
{
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
memoryStream = new System.IO.MemoryStream();
System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
xmlWriterSettings.Encoding = encoding;
System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
Serializer.Serialize(xmlWriter, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
and this object (gen'd from xsd2code):
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Com.Foo.Request")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "Com.Foo.Request", IsNullable = false)]
public partial class REQUEST_GROUP
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<REQUESTING_PARTY> rEQUESTING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private RECEIVING_PARTY rECEIVING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private SUBMITTING_PARTY sUBMITTING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private REQUEST rEQUESTField;
[EditorBrowsable(EditorBrowsableState.Never)]
private string iDField;
public REQUEST_GROUP()
{
this.rEQUESTField = new REQUEST();
this.sUBMITTING_PARTYField = new SUBMITTING_PARTY();
this.rECEIVING_PARTYField = new RECEIVING_PARTY();
this.rEQUESTING_PARTYField = new List<REQUESTING_PARTY>();
this.IDField = "2.1";
}
}
Output from the Serialize with an encode of utf-8:
<?xml version="1.0" encoding="utf-8"?>
<REQUEST_GROUP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="2.1" xmlns="Com.Foo.Request">
<RECEIVING_PARTY />
<SUBMITTING_PARTY />
<REQUEST LoginAccountIdentifier="xxx" LoginAccountPassword="yyy" _RecordIdentifier="" _JobIdentifier="">
<REQUESTDATA>
<PROPERTY_INFORMATION_REQUEST _SpecialInstructionsDescription="" _ActionType="Submit">
<_DATA_PRODUCT _ShortSubjectReport="Y" />
<_PROPERTY_CRITERIA _City="Sunshine City" _StreetAddress2="" _StreetAddress="123 Main Street" _State="CA" _PostalCode="12345">
<PARSED_STREET_ADDRESS />
</_PROPERTY_CRITERIA>
<_SEARCH_CRITERIA />
<_RESPONSE_CRITERIA />
</PROPERTY_INFORMATION_REQUEST>
</REQUESTDATA>
</REQUEST>
</REQUEST_GROUP>
EDIT
Question 1: How do I decorate the class in such a fashion, or manipulate the serializer to get rid of all the namespaces in the REQUEST_GROUP node during processing, NOT post-processing with xslt or regex.
Question 2: Bonus point if you could add the doc type too.
Thank you.
You can remove the namespaces like this:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
ns.Add(string.Empty, "Com.Foo.Request");
Serializer.Serialize(xmlWriter, this, ns);
As for adding the doctype, I know it's possible to make a custom XmlWriter and just override WriteStartDocument with a method that makes a call to WriteDocType, but I kind of hope someone else knows an easier way than that.
EDIT: Incidentally, I strongly recommend using using:
using(System.Xml.XmlWriter xmlWriter = XmlWriter.Create(etc.))
{
// use it here.
}
It automatically handles tidying up of the streams by calling the Dispose method when the block ends.
If you just want to remove the namespace aliases, then as already shown you can use XmlSerializerNamespaces to force XmlSerializer to use the namespace explicitly (i.e. xmlns="blah") on each element, rather than declaring an alias and using the alias instead.
However, regardless of what you do with the aliases, the fundamental name of that element is REQUEST_GROUP in the Com.Foo.Request namespace. You can't remove the namespace completely without that representing a breaking change to the underlying data - i.e. somebody somewhere is going to get an exception (due to getting data it didn't expect - specifically REQUEST_GROUP in the root namespace). In C# terms, it is the difference between System.String and My.Custom.String - sure, they are both called String, but that is just their local name.
If you want to remove all traces of the namespace, then a pragmatic option would be to edit away the Namespace=... entries from [XmlRoot(...)] and [XmlType(...)] (plus anywhere else that isn't shown in the example).
If the types are outside of your control, you can also do this at runtime using XmlAttributeOverrides - but a caveat: if you create an XmlSerializer using XmlAttributeOverrides you must cache and re-use it - otherwise your AppDomain will leak (it creates assemblies on the fly per serializer in this mode, and assemblies cannot be unloaded).
I have a .net Web Api 2 application that delivers data in XML.
My problem:
One of my classes looks like this:
public class Horse
{
public string Name { get;set; }
public string Category { get;set; }
}
When i serialize this, the result is:
<Horse>
<Name>Bobo</Name>
<Category>LargeAnimal</Category>
</Horse>
What i want is to wrap all outgoing XML content with a root element like this:
<Animal>
<Horse>
.....
</Horse>
</Animal>
I was hoping to do this in a custom XmlFormatter. But i can't seem to figure out how to append a root element on the writestream.
What is the best way to resolve this issue?
I have tried tweaking this answer to work in my custom xmlserializer, but doesn't seem to work. How to add a root node to an xml?
( I had a really short amount of time to write this question, so if anything is missing, please leave a comment.)
So.. Tweaked the answer to this question: How to add a root node to an xml? to work with my XmlFormatter.
The following code works, although i feel this is a hackish approach.
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
XmlSerializer xs = new XmlSerializer(type);
XmlDocument temp = new XmlDocument(); //create a temporary xml document
var navigator = temp.CreateNavigator(); //use its navigator
using (var w = navigator.AppendChild()) //to get an XMLWriter
xs.Serialize(w, value); //serialize your data to it
XmlDocument xdoc = new XmlDocument(); //init the main xml document
//add xml declaration to the top of the new xml document
xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "utf-8", null));
//create the root element
var animal = xdoc.CreateElement("Animal");
animal.InnerXml = temp.InnerXml; //copy the serialized content
xdoc.AppendChild(animal);
using (var xmlWriter = new XmlTextWriter(writeStream, encoding))
{
xdoc.WriteTo(xmlWriter);
}
});
}
I'm trying to validate a serialized WCF Proxy class using an Xsd.
I've noticed that the generated Xml, isn't including the namespace on the parent element, but child elements have it. This means my validation throws could not find schema information for the element type errors.
If I manually add a default namespace, then the schema validation works.
My question is, if the request object has a serialization attribute for the namespace, why isn't that being generated automatically?
This is how I generate the serialized Xml for the proxy:
var path = #"C:\DataRequest.xml";
var data = new DataRequest();
using (var fileWriter = new StreamWriter(path))
{
var serializer = new XmlSerializer(data.GetType());
serializer.Serialize(fileWriter, data);
fileWriter.Close();
}
This produces the following DataRequest.xml:
<DataRequest>
<Data xmlns="urn:some:name:space">
<Id>1</Id>
</Data>
</DataRequest>
Here's my request object with the namespace serialization attribute:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")]
<other attributes I snipped>
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:some:name:space")]
public partial class DataRequest : object, System.ComponentModel.INotifyPropertyChanged {
I found that when using the XmlSerializer, to get the namespace at the root, one needs to apply the XmlRootAttribute to the target class.
I fixed the issue by dynamically getting the Namespace value when serializing.
Here's the modified function:
var dataType = data.GetType();
var xmlAttribute = (XmlTypeAttribute)Attribute.GetCustomAttribute(dataType, typeof(XmlTypeAttribute));
XNamespace ns = xmlAttribute.Namespace;
using (var fileWriter = new StreamWriter(filePath))
{
var xSerializer = new XmlSerializer(dataType, ns.NamespaceName);
xSerializer.Serialize(fileWriter, data);
fileWriter.Close();
}
The code came from this SO answer:
How can I dynamically read a classes XmlTypeAttribute to get the Namespace?
Given this generic serialization code:
public virtual string Serialize(System.Text.Encoding encoding)
{
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
memoryStream = new System.IO.MemoryStream();
System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
xmlWriterSettings.Encoding = encoding;
System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
Serializer.Serialize(xmlWriter, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
and this object (gen'd from xsd2code):
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Com.Foo.Request")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "Com.Foo.Request", IsNullable = false)]
public partial class REQUEST_GROUP
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<REQUESTING_PARTY> rEQUESTING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private RECEIVING_PARTY rECEIVING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private SUBMITTING_PARTY sUBMITTING_PARTYField;
[EditorBrowsable(EditorBrowsableState.Never)]
private REQUEST rEQUESTField;
[EditorBrowsable(EditorBrowsableState.Never)]
private string iDField;
public REQUEST_GROUP()
{
this.rEQUESTField = new REQUEST();
this.sUBMITTING_PARTYField = new SUBMITTING_PARTY();
this.rECEIVING_PARTYField = new RECEIVING_PARTY();
this.rEQUESTING_PARTYField = new List<REQUESTING_PARTY>();
this.IDField = "2.1";
}
}
Output from the Serialize with an encode of utf-8:
<?xml version="1.0" encoding="utf-8"?>
<REQUEST_GROUP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="2.1" xmlns="Com.Foo.Request">
<RECEIVING_PARTY />
<SUBMITTING_PARTY />
<REQUEST LoginAccountIdentifier="xxx" LoginAccountPassword="yyy" _RecordIdentifier="" _JobIdentifier="">
<REQUESTDATA>
<PROPERTY_INFORMATION_REQUEST _SpecialInstructionsDescription="" _ActionType="Submit">
<_DATA_PRODUCT _ShortSubjectReport="Y" />
<_PROPERTY_CRITERIA _City="Sunshine City" _StreetAddress2="" _StreetAddress="123 Main Street" _State="CA" _PostalCode="12345">
<PARSED_STREET_ADDRESS />
</_PROPERTY_CRITERIA>
<_SEARCH_CRITERIA />
<_RESPONSE_CRITERIA />
</PROPERTY_INFORMATION_REQUEST>
</REQUESTDATA>
</REQUEST>
</REQUEST_GROUP>
EDIT
Question 1: How do I decorate the class in such a fashion, or manipulate the serializer to get rid of all the namespaces in the REQUEST_GROUP node during processing, NOT post-processing with xslt or regex.
Question 2: Bonus point if you could add the doc type too.
Thank you.
You can remove the namespaces like this:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
ns.Add(string.Empty, "Com.Foo.Request");
Serializer.Serialize(xmlWriter, this, ns);
As for adding the doctype, I know it's possible to make a custom XmlWriter and just override WriteStartDocument with a method that makes a call to WriteDocType, but I kind of hope someone else knows an easier way than that.
EDIT: Incidentally, I strongly recommend using using:
using(System.Xml.XmlWriter xmlWriter = XmlWriter.Create(etc.))
{
// use it here.
}
It automatically handles tidying up of the streams by calling the Dispose method when the block ends.
If you just want to remove the namespace aliases, then as already shown you can use XmlSerializerNamespaces to force XmlSerializer to use the namespace explicitly (i.e. xmlns="blah") on each element, rather than declaring an alias and using the alias instead.
However, regardless of what you do with the aliases, the fundamental name of that element is REQUEST_GROUP in the Com.Foo.Request namespace. You can't remove the namespace completely without that representing a breaking change to the underlying data - i.e. somebody somewhere is going to get an exception (due to getting data it didn't expect - specifically REQUEST_GROUP in the root namespace). In C# terms, it is the difference between System.String and My.Custom.String - sure, they are both called String, but that is just their local name.
If you want to remove all traces of the namespace, then a pragmatic option would be to edit away the Namespace=... entries from [XmlRoot(...)] and [XmlType(...)] (plus anywhere else that isn't shown in the example).
If the types are outside of your control, you can also do this at runtime using XmlAttributeOverrides - but a caveat: if you create an XmlSerializer using XmlAttributeOverrides you must cache and re-use it - otherwise your AppDomain will leak (it creates assemblies on the fly per serializer in this mode, and assemblies cannot be unloaded).
I have an XML document that contains the following structure:
Its more or less a collection of Events:
<Events>
<Event>
<DateTime></DateTime>
<EventType></EventType>
<Result></Result>
<Provider></Provider>
<ErrorMessage></ErrorMessage>
<InnerException></InnerException>
</Event>
</Events>
In C# I have a persistent object called Event:
Now given that the document already exists, and saved to file... I call :
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(dataPath);
Now how can I add a new Event item to events?
I've got a strongly typed Event item in my C# code, and want it inserted into the Events collection in the XML object as last child.
I guess this is what I am really asking : https://stackoverflow.com/questions/1457033/c-insert-a-strongly-typed-object-as-node-in-existing-xml-document
Take a look at the Xml Serialization attributes.
You can do this:
[XmlRoot("Event")]
public class Event
{
[XmlElement("DateTime")]
public string DateTime
{
get;
set;
}
[XmlElement("EventType")]
public EnumReportingEventType EventType
{
get;
set;
}
[XmlElement("Result")]
public EnumReportingResult Result
{
get;
set;
}
[XmlElement("Provider")]
public string Provider
{
get;
set;
}
[XmlElement("ErrorMessage")]
public string ErrorMessage
{
get;
set;
}
[XmlElement("InnerException")]
public string InnerException
{
get;
set;
}
}
In fact, if the properties of your class have the same name as the elements in your Xml, you do not have to use the XmlElement attributes.
Then, you can use the XmlSerializer to serialize and deserialize.
Edit:
Then, wouldn't it be better to create a type which represent the entire type that is stored in the existing xml ?
Deserialize it, give a value to the additional property, and serialize it back ?
Just hand code it using the XmlDocument. You can add a class that does the conversion or insertion into the document you are going to save.
This is based on the restriction of .net 2.0 and what you said in these comments:
#Fred, I want to try minimize the write time, hence the reason for not writing all at once, the less write time in this app, the less chance of file corruption. – JL 16 mins ago
Why do you think you have a lot of chance of file corruption ? – Frederik Gheysels 9 mins ago
From existing test results on code I already have using serialization as a whole.
If you are using .Net 3.5 you can use Linq to XML, something like the following will work
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Event document"),
new XElement("Events",
new XElement ("Event",
new XElement("DateTime", event.DateTime),
new XElement("EventType", event.EventType),
new XElement("Result", event.Result),
new XElement("Provider", event.Provider),
new XElement("ErrorMessage", event.ErrorMessage),
new XElement("InnerException", event.InnerException)
)
));
doc.Save(#"c:\sample.xml");
If you have an existing xml document that you want to append to somthing like the following is required.
XDocument doc = XDocument.Load(#"c:\sample.xml");
XElement events = doc.Element(XName.Get("Events"));
events.Add(new XElement ("Event",
new XElement("DateTime", event.DateTime),
new XElement("EventType", event.EventType),
new XElement("Result", event.Result),
new XElement("Provider", event.Provider),
new XElement("ErrorMessage", event.ErrorMessage),
new XElement("InnerException", event.InnerException)
));
doc.Save(#"c:\sample.xml");
Insert a XmlElement field near the end of the "Event" class like so:
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement Any { get; set }
You can name it whatever you want as long as you have the "XmlAnyElementAttribute" on it.
You can use something like the following to serialize a strongly-typed object into this field:
MyParentObject parent = new MyParentObject(){ ... };
MyObject obj = new MyObject(){ /*... initialize*/ };
XmlSerializer ser = new XmlSerializer(typeof(MyObject));
XmlDocument doc = new XmlDocument();
using (StringWriter sw = new StringWriter())
{
ser.Serialize(sw, obj);
doc.LoadXml(sw.ToString());
}
parent.Any = (XmlElement)doc.DocumentElement;
The serialized XML will append nicely to your class, it will event include the correct namespaces.
Assuming that your Event class can already be serialized the way you want using XmlSerializer, you can do the following:
XmlSerializer ser = new XmlSerializer(typeof(Event));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(dataPath);
Event evt = ...;
XmlDocument evtDoc = new XmlDocument();
using (XmlWriter writer = evtDoc.CreateNavigator().AppendChild())
{
ser.Serialize(writer, evt);
}
XmlNode evtNode = evtDoc.RemoveChild(evtDoc.DocumentElement);
XmlNode events = xmlDoc.SelectSingleNode("/Events");
events.AppendChild(evtNode);
What you are looking to do is something like:
doc.ChildNode[0].AppendChild(MethodToReturnADeserializedObject(event));
Create a method to deserialize the event object into an xml node. Then use AppendChild to insert that as the last element amongst it's child nodes.