XML remove nil attribute - c#

I am doing some XmlOverrides and setting properties to nullable (F1 in this case). I would want to delete nil="true" and the namespace around this attribute, but I can't remove it with any method I've tried.
Nested n = new Nested();
n.Nest = "2";
n.F1 = null;
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes att = new XmlAttributes();
XmlElementAttribute el = new XmlElementAttribute("F1");
el.IsNullable = true;
att.XmlElements.Add(el);
overrides.Add(typeof(Nested), "F1", att);
XmlSerializer xs = new XmlSerializer(typeof(Nested), overrides);
var faultDocument = new XmlDocument();
var nav = faultDocument.CreateNavigator();
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
using (var writer = nav.AppendChild())
{
xs.Serialize(writer, n, ns);
}
XML I get is like this:
<Nested>
<F1 p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance"/>
<Nest>2</Nest>
</Nested>
But I would want that F1 element be empty, without nil and xmlns
<Nested>
<F1/>
<Nest>2</Nest>
</Nested>

Try to add in Nested class [XmlElement(ElementName = "F1", IsNullable = true)] in property F1.

I managed to solve it, not sure if it's the best way to do it:
private static void RemoveNils(XElement elem)
{
string nilNamespace = "{http://www.w3.org/2001/XMLSchema-instance}nil";
//first condition should be enough, the rest are just fail-safes
if (elem.Attributes().Any(name => name.Name == nilNamespace) && elem.Elements().Count() == 0 && elem.IsEmpty)
{
elem.Attributes().Remove();
return;
}
foreach (XElement el in elem.Elements())
RemoveNils(el);
}

Try this
Nested n = new Nested();
n.Nest = "2";
n.F1 = null;
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes att = new XmlAttributes();
XmlElementAttribute el = new XmlElementAttribute("F1");
el.IsNullable = true;
att.XmlElements.Add(el);
overrides.Add(typeof(Nested), "F1", att);
XmlSerializer xs = new XmlSerializer(typeof(Nested));
var faultDocument = new XmlDocument();
var nav = faultDocument.CreateNavigator();
StringWriter strWriter = new StringWriter();
XmlTextWriter TextWriter = new XmlTextWriter(strWriter);
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
xs.Serialize(TextWriter, n, ns);
ReqXml = strWriter.ToString();
TextWriter.Close();
return ReqXml;

Related

Xsd schema generation - type 'string' is not declared, or is not a simple type

I am creating a xml schema using schema object model, on step "set.compile()", I get the above mentioned error. I guess its related to the base type name of simpletype elements, help!
Is there any method, we can actually see what is giong in while set.compile() method compiles the code??
XmlSchemaSimpleType namesimpletype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction res = new XmlSchemaSimpleTypeRestriction();
res.BaseTypeName = new XmlQualifiedName("string","");
XmlSchemaMinLengthFacet facet1 = new XmlSchemaMinLengthFacet();
facet1.Value = "3";
XmlSchemaMaxLengthFacet facet2 = new XmlSchemaMaxLengthFacet();
facet2.Value = "10";
res.Facets.Add(facet1);
res.Facets.Add(facet2);
namesimpletype.Content = res;
XmlSchemaSimpleType phonetype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction phone_res = new XmlSchemaSimpleTypeRestriction();
phone_res.BaseTypeName = new XmlQualifiedName("string", "");
XmlSchemaMinLengthFacet phone_facet1 = new XmlSchemaMinLengthFacet();
phone_facet1.Value = "4";
XmlSchemaMaxLengthFacet phone_facet2 = new XmlSchemaMaxLengthFacet();
phone_facet2.Value = "20";
phone_res.Facets.Add(phone_facet1);
phone_res.Facets.Add(phone_facet2);
phonetype.Content = phone_res;
XmlSchemaSimpleType notetype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction note_res = new XmlSchemaSimpleTypeRestriction();
note_res.BaseTypeName = new XmlQualifiedName("string", "");
XmlSchemaMinLengthFacet note_facet1 = new XmlSchemaMinLengthFacet();
note_facet1.Value = "0";
XmlSchemaMaxLengthFacet note_facet2 = new XmlSchemaMaxLengthFacet();
facet2.Value = "50";
note_res.Facets.Add(note_facet1);
note_res.Facets.Add(note_facet2);
notetype.Content = note_res;
XmlSchemaComplexType employeetype = new XmlSchemaComplexType();
XmlSchemaSequence emp_seq = new XmlSchemaSequence();
XmlSchemaElement firstname = new XmlSchemaElement();
firstname.Name = "firstname";
firstname.SchemaType = namesimpletype;
XmlSchemaElement lastname = new XmlSchemaElement();
lastname.Name = "lastname";
lastname.SchemaType = namesimpletype;
XmlSchemaElement homephone = new XmlSchemaElement();
homephone.Name = "homePhone";
homephone.SchemaType = phonetype;
XmlSchemaElement notes = new XmlSchemaElement();
notes.Name = "notes";
notes.SchemaType = notetype;
emp_seq.Items.Add(firstname);
emp_seq.Items.Add(lastname);
emp_seq.Items.Add(homephone);
emp_seq.Items.Add(notes);
employeetype.Particle = emp_seq;
/* XmlSchemaAttribute employeeid = new XmlSchemaAttribute();
employeeid.Name = "employeeid";
employeeid.SchemaTypeName = new XmlQualifiedName("int", "");
employeeid.Use = XmlSchemaUse.Required;
employeetype.Attributes.Add(employeeid);
*/
XmlSchemaComplexType complextype = new XmlSchemaComplexType();
complextype.Name = "employees";
XmlSchemaElement employee = new XmlSchemaElement();
employee.Name = "employee";
employee.SchemaType = employeetype;
employee.MinOccurs = 0;
employee.MaxOccursString = "unbounded";
XmlSchemaSequence emps_seq = new XmlSchemaSequence();
emps_seq.Items.Add(employee);
complextype.Particle = emps_seq;
XmlSchema schema = new XmlSchema();
schema.Items.Add(complextype);
XmlSchemaSet set = new XmlSchemaSet();
set.Add(schema);
set.Compile();
/* try
{
set.Compile();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} */
FileStream stream = new FileStream(textBox1.Text, FileMode.Open);
XmlTextWriter writer = new XmlTextWriter(stream, null);
schema.Write(writer);
writer.Close();
Try to use full name with namespace:
res.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
See Example section on page http://msdn.microsoft.com/en-us/library/1c4sky9s(v=vs.110).aspx

Can I add a prefix to root element when I have no control over the serialized .NET type?

I have a library that contains a .NET type. The library has its own configuration (annotations etc) for serializing the type to XML, and I do not have the source code.
No matter what I do, I could not manage to add the prefix I want to add to the root element of the output XML. Using XmlSerializerNamespaces made no difference. Here is a snippet that shows my code at the moment:
var comp = row[0] as LibraryType;
var ser = new XmlSerializer(comp.GetType());
var strWriter = new StringWriter();
var xmlWriter = XmlWriter.Create(strWriter);
ser.Serialize(xmlWriter, comp);
string serXml = strWriter.ToString();
Is there a way in configure XMLSerializer to create an xlm output for root such as
<lt:LibraryType ....
instead of the current
<LibraryType .....
I'm getting ?
Lets say that your library type looks like
public class Foo
{
public int i { get; set; }
}
public class Bar
{
public Foo Foo { get; set; }
}
then serialization should look like
var comp = new Bar {Foo = new Foo()};
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof (Bar), new XmlAttributes {XmlRoot = new XmlRootAttribute {Namespace = "http://tempuri.org"}});
// in case you want to remove prefix from members
var emptyNsAttribute = new XmlAttributes();
emptyNsAttribute.XmlElements.Add(new XmlElementAttribute { Namespace = "" });
overrides.Add(typeof(Bar), "Foo", emptyNsAttribute);
// if you actual library type contains more members, then you have to list all of them
var ser = new XmlSerializer(comp.GetType(), overrides);
var strWriter = new StringWriter();
var xmlWriter = XmlWriter.Create(strWriter);
var ns = new XmlSerializerNamespaces();
ns.Add("lt", "http://tempuri.org");
ser.Serialize(xmlWriter, comp, ns);
string serXml = strWriter.ToString();
and the output will be
<?xml version="1.0" encoding="utf-16"?><lt:Bar xmlns:lt="http://tempuri.org"><Foo><i>0</i></Foo></lt:Bar>

Filtering by higher nodes attributes linq

I am trying to serialize and insert a new created object into a specific child node of an XDocument. I've managed to accomplish this but my code seems smelly.
How can I test against a higher nodes attribute value without chaining through the Parent property like I have done below?
XDocument xdoc = XDocument.Load(path);
var elements = (
from doc in xdoc.Descendants("Unit")
where doc.Parent.Parent.Attribute("name").Value == _UnitTypeName &&
doc.Parent.Parent.Parent.Parent.Attribute("name").Value == _UnitCategoryN
doc.Parent.Parent.Parent.Parent.Parent.Parent.Attribute("name").Value ==
select doc
);
foreach (var element in elements)
{
Unit unit =
new Unit
{
Armour = _Armour,
Attacks = _Attacks,
BallisticSkill = _BallisticSkill,
Composition = _Composition,
DedicatedTransport = _DedicatedTransport,
Initiative = _Initiative,
Leadership = _Leadership,
Options = _Options,
SaveThrow = _SaveThrow,
SpecialRules = _SpecialRules,
Strength = _Strength,
Toughness = _Toughness,
UnitName = _UnitName,
Weapons = _Weapons,
WeaponSkill = _WeaponSkill,
Wounds = _Wounds,
Points = _Points
};
XmlSerializer serializer = new XmlSerializer(typeof(Unit));
using (var stream = new MemoryStream())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(stream, unit, ns);
stream.Position = 0;
//using (XmlReader reader = XmlReader.Create(stream))
//{
// XElement xe = XElement.Load(reader);
XElement xe = XElement.Load(#"C:\Test\tempfile.xml"); // For some reason loading via MemoryStream messes with xml formatting
element.AddBeforeSelf(xe);
//}
}
break;
}
xdoc.Save(path);
This is the structure of the XML Document:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArmy>
<Army name="Tyranid">
<unit-category>
<UnitCategory name="Troops">
<unit-type>
<UnitType name="Infantry">
<unit>
<Unit points="5" name="Hornmagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
<Amount>0</Amount>
</Unit>
<Unit points="5" name="Termagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
<Amount>0</Amount>
</Unit>
</unit>
</UnitType>
</unit-type>
</UnitCategory>
</unit-category>
</Army>
</ArrayOfArmy>
You can use compound from clauses, which is similar to using nested foreach loops:
var elements = (
from army in xdoc.Descendants("Army")
where army.Attribute("name").Value == _ArmyName
from unitCategory in army.Descendants("UnitCategory")
where unitCategory.Attribute("name").Value == _UnitCategoryName
from unitType in unitCategory.Descendants("UnitType")
where unitType.Attribute("name").Value == _UnitTypeName
from unit in unitType.Descendants("Unit")
select unit
);
Note: the method syntax equivalent is SelectMany().

Convert xmlstring into XmlNode

i have one xml string like this
string stxml="<Status>Success</Status>";
I also creaated one xml document
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
i need an output like this.
<StatusList>
<Status>Success</Status>
</StatusList>
How can i achieve this.if we using innerhtml,it will insert.But i want to insert xml string as a xmlnode itself
A very simple way to achieve what you are after is to use the often overlooked XmlDocumentFragment class:
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode rootNode = doc.CreateElement("StatusList");
doc.AppendChild(rootNode);
//Create a document fragment and load the xml into it
XmlDocumentFragment fragment = doc.CreateDocumentFragment();
fragment.InnerXml = stxml;
rootNode.AppendChild(fragment);
Using Linq to XML:
string stxml = "<Status>Success</Status>";
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("StatusList", XElement.Parse(stxml)));
You could instead use the XElement class:
string stxml = "<Status>Success</Status>";
var status = XElement.Parse(stxml);
var statusList = new XElement("StatusList", status);
var output = statusList.ToString(); // looks as you'd like
If you want to write the new statusList content to a file:
statusList.Save(#"C:\yourFile.xml", SaveOptions.None);
you ca try it using xmlwriter
using (XmlWriter writer = XmlWriter.Create("new.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("StatusList");
writer.WriteElementString("Status", "Success"); // <-- These are new
writer.WriteEndDocument();
}
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;
public class MyClass
{
public static void RunSnippet()
{
XmlNode node = default(XmlNode);
if(node == null)
Console.WriteLine(bool.TrueString);
if(node != null)
Console.WriteLine(bool.FalseString);
XmlDocument doc = new XmlDocument();
node = doc.CreateNode (XmlNodeType.Element,"Query", string.Empty);
node.InnerXml=#"<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>";
string xmlData = ToXml<XmlNode>(node);
Console.WriteLine(xmlData);
XmlNode node1 = ConvertFromString(typeof(XmlNode), #"<Query><Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where></Query>") as XmlNode;
if(node1 == null)
Console.WriteLine(bool.FalseString);
if(node1 != null)
Console.WriteLine(bool.TrueString);
string xmlData1 = ToXml<XmlNode>(node1);
Console.WriteLine(xmlData1);
}
public static string ToXml<T>(T t)
{
string Ret = string.Empty;
XmlSerializer s = new XmlSerializer(typeof(T));
using (StringWriter Output = new StringWriter(new System.Text.StringBuilder()))
{
s.Serialize(Output, t);
Ret = Output.ToString();
}
return Ret;
}
public static object ConvertFromString(Type t, string sourceValue)
{
object convertedVal = null;
Type parameterType = t;
if (parameterType == null) parameterType = typeof(string);
try
{
// Type t = Type.GetType(sourceType, true);
TypeConverter converter = TypeDescriptor.GetConverter(parameterType);
if (converter != null && converter.CanConvertFrom(typeof(string)))
{
convertedVal = converter.ConvertFromString(sourceValue);
}
else
{
convertedVal = FromXml(sourceValue, parameterType);
}
}
catch { }
return convertedVal;
}
public static object FromXml(string Xml, Type t)
{
object obj;
XmlSerializer ser = new XmlSerializer(t);
using (StringReader stringReader = new StringReader(Xml))
{
using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader))
{
obj = ser.Deserialize(xmlReader);
}
}
return obj;
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
From my experience it is always better to work with unique id's i suggest u look in that situation first and then come back to this page, and research/position my code for a try if u did not yet have a solution for it.
I just finished it on my side for my own project, i have modified abit to look more integrated for your project.
Good luck. Sorry for the late response ;-)
XmlDocument xDoc = new XmlDocument();
string Bingo = "Identification code";
xDoc.Load(pathFile);
XmlNodeList idList = xDoc.GetElementsByTagName("id");
XmlNodeList statusList = xDoc.GetElementsByTagName("Status");
for (int i = 0; i < idList.Count; i++)
{
StatusNode = "<Status>fail</Status>";
XmlDocumentFragment fragment = xDoc.CreateDocumentFragment();
fragment.InnerXml = StatusNode;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
if (statusList[i].InnerText == Bingo)
{
StatusNode = "<Status>Succes!</Status>";
fragment.InnerXml = Status;
statusList[i].InnerXml = "";
statusList[i].AppendChild(fragment);
}
}
xDoc.Save(pathFile);

XML Serialization of List<T> - XML Root

First question on Stackoverflow (.Net 2.0):
So I am trying to return an XML of a List with the following:
public XmlDocument GetEntityXml()
{
StringWriter stringWriter = new StringWriter();
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
List<T> parameters = GetAll();
serializer.Serialize(xmlWriter, parameters);
string xmlResult = stringWriter.ToString();
xmlDoc.LoadXml(xmlResult);
return xmlDoc;
}
Now this will be used for multiple Entities I have already defined.
Say I would like to get an XML of List<Cat>
The XML would be something like:
<ArrayOfCat>
<Cat>
<Name>Tom</Name>
<Age>2</Age>
</Cat>
<Cat>
<Name>Bob</Name>
<Age>3</Age>
</Cat>
</ArrayOfCat>
Is there a way for me to get the same Root all the time when getting these Entities?
Example:
<Entity>
<Cat>
<Name>Tom</Name>
<Age>2</Age>
</Cat>
<Cat>
<Name>Bob</Name>
<Age>3</Age>
</Cat>
</Entity>
Also note that I do not intend to Deserialize the XML back to List<Cat>
There is a much easy way:
public XmlDocument GetEntityXml<T>()
{
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("TheRootElementName"));
ser.Serialize(writer, parameters);
}
return xmlDoc;
}
If I understand correctly, you want the root of the document to always be the same, whatever the type of element in the collection ? In that case you can use XmlAttributeOverrides :
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
attr.XmlRoot = new XmlRootAttribute("TheRootElementName");
overrides.Add(typeof(List<T>), attr);
XmlSerializer serializer = new XmlSerializer(typeof(List<T>), overrides);
List<T> parameters = GetAll();
serializer.Serialize(xmlWriter, parameters);
A better way to the same thing:
public XmlDocument GetEntityXml<T>()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
attr.XmlRoot = new XmlRootAttribute("TheRootElementName");
overrides.Add(typeof(List<T>), attr);
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), overrides);
List<T> parameters = GetAll<T>();
ser.Serialize(writer, parameters);
}
return xmlDoc;
}
so simple....
public static XElement ToXML<T>(this IList<T> lstToConvert, Func<T, bool> filter, string rootName)
{
var lstConvert = (filter == null) ? lstToConvert : lstToConvert.Where(filter);
return new XElement(rootName,
(from node in lstConvert
select new XElement(typeof(T).ToString(),
from subnode in node.GetType().GetProperties()
select new XElement(subnode.Name, subnode.GetValue(node, null)))));
}

Categories