I have a object obj with 2 properties p1, p2. and a XElement like:
<root><AA><BB>BB</BB></AA></root>
I'd like to make my Xelement as:
<root><AA><BB>BB</BB><CC><p1>val1</p1><p2>val2</p2></CC></AA></root>
I make a new XElement from obj
XElement x = new XElement("CC",new XElement("p1", obj.p1),new XElement("p2", obj.p2));
and insert it in AA element. is ther a better way by serializing my obj and convert it to XElement? (Because My object can change in the future) . Thanks for any help.
Here is my attempt to use XmlSerializer:
XElement xelem = reqRet.RequestDefinition;
xelem.Descendants("AA").ToList().ForEach(reqitem =>
{
using (MemoryStream ms = new MemoryStream())
{
using (TextWriter tw = new StreamWriter(ms))
{
XmlSerializer ser = new XmlSerializer(typeof(obj));
ser.Serialize(tw, ObjVAL);
schElem = new XElement( XElement.Parse(Encoding.ASCII.GetString(ms.ToArray())));
reqitem.Add(schElem);
}
}
reqitem.Add(schElem);
});
Since you're open to using XmlSerializer, use the XmlRoot attribute; try adding the following to your class declaration:
[XmlRoot(Namespace = "www.contoso.com",
ElementName = "CC",
DataType = "string",
IsNullable=true)]
public class MyObj
{
...
See https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute%28v=vs.110%29.aspx for more information.
After that, you can use this code:
XElement xelem = XElement.Parse("<root><AA><BB>BB</BB></AA></root>");
MyObj myObj = new MyObj();
XmlSerializer ser = new XmlSerializer(typeof(MyObj));
foreach (XElement reqitem in xelem.Descendants("AA"))
{
using (MemoryStream ms = new MemoryStream())
{
ser.Serialize(ms, myObj);
reqitem.Add(XElement.Parse(Encoding.UTF8.GetString(ms.ToArray())));
}
}
This gives the desired output.
If you want to remove the XMLNS declarations, you can use .Attributes.Remove() after creating the XElement.
Related
in this xml
<Roots>
<Root Name="cab">element_list</Root>
</Roots>
I want to get the value of attribute Name which is cab and element_list
I have this code
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Root Name=\"cab\">element_list</Root>");
XmlElement root = doc.DocumentElement;
var values = doc.Descendants("Roots").Select(x => new { Name = (string)x.Attribute("Name"), List = (string)x }).ToList();
What I get when I run the debugger is
values> Name = "null", List = "element_list"
I am not understanding why I am getting a null value when I should be getting cab for the attribute Name
XDocument is much easier to work with compared to XmlDocument. Perhaps consider switching to it?
public static void ParseXml()
{
string str = "<Roots><Root Name=\"cab\">element_list</Root></Roots>";
using TextReader textReader = new StringReader(str);
XDocument doc = XDocument.Load(textReader);
var val = doc.Element("Roots").Element("Root").Attribute("Name").Value;
}
Using the bog-standard System.Xml.Serialization.XmlSerializer, I am serializing an object who's class inherits from another. Inspecting the resulting XML, the root node is being given the attributes "p1:type" and "xmlns:p1":
<ApiSubmission ApiVersion="1" CustId="100104" p1:type="OrderConfirmationApiSubmission"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance">
...
</ApiSubmission>
Is there a nice way to remove these attributes?
So I came upon this same issue ~5 years after this question was originally asked and was disappointed that no one had answered. After searching around I cobbled something together that allows me to strip out the type attribute in a derived class.
internal static string SerializeObject(object objectToSerialize, bool OmitXmlDeclaration = true, System.Type type = null, bool OmitType = false, bool RemoveAllNamespaces = true)
{
XmlSerializer x;
string output;
if (type != null)
{
x = new XmlSerializer(type);
}
else
{
x = new XmlSerializer(objectToSerialize.GetType());
}
XmlWriterSettings settings = new XmlWriterSettings() { Indent = false, OmitXmlDeclaration = OmitXmlDeclaration, NamespaceHandling = NamespaceHandling.OmitDuplicates };
using (StringWriter swriter = new StringWriter())
using (XmlWriter xmlwriter = XmlWriter.Create(swriter, settings))
{
x.Serialize(xmlwriter, objectToSerialize);
output = swriter.ToString();
}
if (RemoveAllNamespaces || OmitType)
{
XDocument doc = XDocument.Parse(output);
if (RemoveAllNamespaces)
{
foreach (var element in doc.Root.DescendantsAndSelf())
{
element.Name = element.Name.LocalName;
element.ReplaceAttributes(GetAttributesWithoutNamespace(element));
}
}
if (OmitType)
{
foreach (var node in doc.Descendants().Where(e => e.Attribute("type") != null))
{
node.Attribute("type").Remove();
}
}
output = doc.ToString();
}
return output;
}
I use this and [XmlInclude] the derived class in the base class. Then OmitType and RemoveAllNamespaces. Essentially the derived class is then treated as if it were the base class.
Calling
List<PC> _PCList = new List<PC>();
...add Pc to PCList..
WriteXML<List<PC>>(_PCList, "ss.xml");
Function
public static void WriteXML<T>(T o, string filename)
{
string filePath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Genweb2\\ADSnopper\\" + filename;
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, o); // error
}
File.WriteAllText(filePath,xmlDoc.InnerXml);
}
inner exception
Unable to cast object of type 'System.Collections.Generic.List1[PC]' to type
'System.Collections.Generic.List1[System.Collections.Generic.List`1[PC]]'.
Please Help
The problem is with the line
XmlSerializer ser = new XmlSerializer(typeof(List<T>), ...
Your T is already List<PC>, and you're trying to create typeof(List<T>), which will translate to typeof(List<List<PC>>). Simply make it typeof(T) instead.
It should be
typeof(T)
instead of
List<T>
XmlSerializer ser = new XmlSerializer(typeof(T), new XmlRootAttribute("TheRootElementName"));
this line in your code causing problem
XmlSerializer ser = new XmlSerializer(typeof(List<T>),
its creating list of list than is not needed
XmlSerializer ser = new XmlSerializer(typeof(T),
either you do above change or do below changes
There is problem with you method you need to change signature to
public static void WriteXML<T>(List<T> o, string filename)
and call method as below
WriteXML<PC>(_PCList, "ss.xml");
By doing above change might resolve your issue.
I'm trying to use the following code to serialize a list of objects into XDocument, but I'm getting an error stating that "Non white space characters cannot be added to content
"
public XDocument GetEngagement(MyApplication application)
{
ProxyClient client = new ProxyClient();
List<Engagement> engs;
List<Engagement> allEngs = new List<Engagement>();
foreach (Applicant app in application.Applicants)
{
engs = new List<Engagement>();
engs = client.GetEngagements("myConnString", app.SSN.ToString());
allEngs.AddRange(engs);
}
DataContractSerializer ser = new DataContractSerializer(allEngs.GetType());
StringBuilder sb = new StringBuilder();
System.Xml.XmlWriterSettings xws = new System.Xml.XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xws))
{
ser.WriteObject(xw, allEngs);
}
return new XDocument(sb.ToString());
}
What am I doing wrong? Is it the XDocument constructor that doesn't take a list of objects? how do solve this?
I would think that last line should be
return XDocument.Parse(sb.ToString());
And it might be an idea to cut out the serializer altogether, it should be easy to directly create an XDoc from the List<> . That gives you full control over the outcome.
Roughly:
var xDoc = new XDocument( new XElement("Engagements",
from eng in allEngs
select new XElement ("Engagement",
new XAttribute("Name", eng.Name),
new XElement("When", eng.When) )
));
The ctor of XDocument expects other objects like XElement and XAttribute. Have a look at the documentation. What you are looking for is XDocument.Parse(...).
The following should work too (not tested):
XDocument doc = new XDocument();
XmlWriter writer = doc.CreateNavigator().AppendChild();
Now you can write directly into the document without using a StringBuilder. Should be much faster.
I have done the job this way.
private void button2_Click(object sender, EventArgs e)
{
List<BrokerInfo> listOfBroker = new List<BrokerInfo>()
{
new BrokerInfo { Section = "TestSec1", Lineitem ="TestLi1" },
new BrokerInfo { Section = "TestSec2", Lineitem = "TestLi2" },
new BrokerInfo { Section = "TestSec3", Lineitem ="TestLi3" }
};
var xDoc = new XDocument(new XElement("Engagements",
new XElement("BrokerData",
from broker in listOfBroker
select new XElement("BrokerInfo",
new XElement("Section", broker.Section),
new XElement("When", broker.Lineitem))
)));
xDoc.Save("D:\\BrokerInfo.xml");
}
public class BrokerInfo
{
public string Section { get; set; }
public string Lineitem { get; set; }
}
I haven't been able to find a question related to my specific problem.
What I am trying to do is take a list of Xml nodes, and directly deserialize them to a List without having to create a class with attributes.
So the xml (myconfig.xml) would look something like this...
<collection>
<item>item1</item>
<item>item2</item>
<item>item3</item>
<item>etc...</item>
</collection>
In the end I would like a list of items as strings.
The code would look like this.
XmlSerializer serializer = new XmlSerializer( typeof( List<string> ) );
using (XmlReader reader = XmlReader.Create( "myconfig.xml" )
{
List<string> itemCollection = (List<string>)serializer.Deserialize( reader );
}
I'm not 100% confident that this is possible, but I'm guessing it should be. Any help would be greatly appreciated.
Are you married to the idea of using a serializer? If not, you can try Linq-to-XML. (.NET 3.5, C# 3 [and higher])
Based on your provided XML file format, this is the simple code.
// add 'using System.Xml.Linq' to your code file
string file = #"C:\Temp\myconfig.xml";
XDocument document = XDocument.Load(file);
List<string> list = (from item in document.Root.Elements("item")
select item.Value)
.ToList();
Ok, interestingly enough I may have found half the answer by serializing an existing List.
The result I got is as follows...
This following code:
List<string> things = new List<string> { "thing1", "thing2" };
XmlSerializer serializer = new XmlSerializer(typeof(List<string>), overrides);
using (TextWriter textWriter = new StreamWriter("things.xml"))
{
serializer.Serialize(textWriter, things);
}
Outputs a result of:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>thing1</string>
<string>thing2</string>
</ArrayOfString>
I can override the root node by passing an XmlAttributeOverrides instance to the second parameter of the XmlSerializer constructor. It is created like this:
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributes = new XmlAttributes { XmlRoot = new XmlRootAttribute("collection") };
overrides.Add( typeof(List<string>), attributes );
This will change "ArrayOfString" to "collection". I still have not figured out how to control the name of the string element.
To customize List element names using XmlSerializer, you have to wrap the list.
[XmlRoot(Namespace="", ElementName="collection")]
public class ConfigWrapper
{
[XmlElement("item")]
public List<string> Items{ get; set;}
}
Usage:
var itemsList = new List<string>{"item1", "item2", "item3"};
var cfgIn = new ConfigWrapper{ Items = itemsList };
var xs = new XmlSerializer(typeof(ConfigWrapper));
string fileContent = null;
using (var sw = new StringWriter())
{
xs.Serialize(sw, cfgIn);
fileContent = sw.ToString();
Console.WriteLine (fileContent);
}
ConfigWrapper cfgOut = null;
using (var sr = new StringReader(fileContent))
{
cfgOut = xs.Deserialize(sr) as ConfigWrapper;
// cfgOut.Dump(); //view in LinqPad
if(cfgOut != null)
// yields 'item2'
Console.WriteLine (cfgOut.Items[1]);
}
Output:
// fileContent:
<?xml version="1.0" encoding="utf-16"?>
<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</collection>
If you don't want to wrap the list, the DataContractSerializer will allow you to custom name the elements if you subclass it:
[CollectionDataContract(Name = "collection", ItemName = "item", Namespace = "")]
public class ConfigWrapper : List<string>
{
public ConfigWrapper() : base() { }
public ConfigWrapper(IEnumerable<string> items) : base(items) { }
public ConfigWrapper(int capacity) : base(capacity) { }
}
Usage And Output:
var cfgIn = new ConfigWrapper{ "item1", "item2", "item3" };
var ds = new DataContractSerializer(typeof(ConfigWrapper));
string fileContent = null;
using (var ms = new MemoryStream())
{
ds.WriteObject(ms, cfgIn);
fileContent = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine (fileContent);
}
// yields: <collection xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><item>item1</item><item>item2</item><item>item3</item></collection>
ConfigWrapper cfgOut = null;
using (var sr = new StringReader(fileContent))
{
using(var xr = XmlReader.Create(sr))
{
cfgOut = ds.ReadObject(xr) as ConfigWrapper;
// cfgOut.Dump(); //view in LinqPad
if(cfgOut != null)
// yields 'item2'
Console.WriteLine (cfgOut[1]);
}
}