adding element to xml file from c#.net - c#

how to add element to a xml file using asp.net& c#.net inweb application

Here's an example of adding a new element to the root node:
XDocument doc = XDocument.Load("test.xml");
doc.Root.Add(new XElement("someNode", "some node value"));
doc.Save("test.xml");

Load the document using XmlDocument class and then modify it as needed. Reference documentation and examples here.

You can use XmlDocument.CreateElement method to create it and then to append it

Related

Cannot convert XElement to XObject

I am trying to serialize an XML file using the following code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(#"D:\myfile.xml");
string jsonStr = JsonConvert.SerializeXNode(xDoc);
but it's not working and I am getting following error on 3rd line
Cannot convert XmlDocument into XObject
I also tried to find the first node and then try to pass it but its also not working.
You're using XmlDocument, which is from the "old" XML API. Json.NET uses the "new" XML API of LINQ to XML. You just need to change how you're loading the XML:
XDocument xml = XDocument.Load(#"D:\myfile.xml");
string json = JsonConvert.SerializeXNode(xml);

Can't load files that has xmlns attribute in the first child with XDocument

I am new to XDocument but I have been looking around for a solution to this problem which I couldn't get to fix.
I need to load some kind of XML files (PNML) that comes this way:
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="id" type ="http://www.pnml.org/version-2009/grammar/ptnet">
..........</net> </pnml>
And I couldn't get to load these kind of files unless I add "xmlns" as an Attribute to the node net .
Meanwhile, the files I create myself has this xmlns attribute, and I can load them without problems.
While, files that are generated from some other software that I need to be able to use from my software doesn't has this "xmlns" attribute, and if I add it myself to the files generated by this software, I can load those files.
Here's the code I am using to Load :
XDocument doc = XDocument.Load(file);
XNamespace ns = #"http://www.pnml.org/version-2009/grammar/pnml";
foreach (XElement element in doc.Element(ns + "pnml")
.Elements("net").Elements("page").Elements("place"))
{ // Do my loading to "place" nodes for example }
But whenever I try to load a file, it just skips my "foreach" statement, and if I add some line before "foreach" like:
string id= (string) doc.Element(ns + "pnml")
.Element("net").Attribute("id");
it says:
Object reference not set to an instance of an object.
Here's an example of a file generated by my code and also can be read from my code:
<?xml version="1.0" encoding="utf-8"?>
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="netid" type="http://www.pnml.org/version-2009/grammar/ptnet" xmlns="">
nodes and information </net> </pnml>
NOTE: I use this code to save my files:
XNamespace ns = #"http://www.pnml.org/version-2009/grammar/pnml";
XDocument doc = new XDocument
(
new XElement(ns+"pnml"
, new XElement("net",new XAttribute("id", net_id), ...));
I found a way to save my files without this "xmlns" attribute, but once I omit it, I can't load it from my code. And the first example I wrote is the standard format and I really need to get ride of the "xmlns" problem.
EDIT: I'm sorry if you got confused, what I want is to be able to load the standard PNML files that doesn't have thise "xmlns" attribute within the "net" node.
What you're missing is that element namespaces are inherited from their parents.
So your XML:
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="id" type ="http://www.pnml.org/version-2009/grammar/ptnet">
...
Contains two elements. One is pnml with the namespace http://www.pnml.org/version-2009/grammar/pnml, and the child is net which also has the namespace http://www.pnml.org/version-2009/grammar/pnml.
With this in mind, your query on the existing XML should be:
doc.Element(ns + "pnml").Elements(ns + "net")...
And your code to generate the XML should be:
new XElement(ns + "pnml",
new XElement(ns + "net", new XAttribute("id", net_id), ...));
Try something like this
var result = doc.Element(ns + "pnml").Descendants().Where(x=>x.Name.LocalName=="net")

How to add an attribute to XML

I am returning an object from a web service. It arrives in XML format -
<DailyTracker xmlns="http://schemas.datacontract.org/2004/07/MSI.Web.MSINet.BusinessEntities">
<ClientId>2147483647</ClientId>
<ClientRosterId>2147483647</ClientRosterId>
<Dept>
<DepartmentID>2147483647</DepartmentID>
<DepartmentName>String content</DepartmentName>
<EmailAddress>String content</EmailAddress>
<Location>2147483647</Location>
<PayCode>String content</PayCode>
</Dept>
etc, etc...
</DailyTracker>
This is coming from an asp.net website using c#. I am returning an object of type DailyTracker.
how can I add an attribute to one of the elements? Is that possible?
Thanks!
Instantiate an XDocument using the XML returned from the service. Get the XElement that you want, then add a new XAttribute to it:
XDocument document = new XDocument(xmlString);
XElement element = document.Element("myElement");
element.Add(new XAttribute("MyAttr", "My Value"));
You can override the Serialization process and add custom attributes to the Serialized XML content similar to the one that is described here

A reasonable way to add attributes to an xml root element in C#

The function "WriteStartElement" does not return anything. I find this a little bizzare.
So up until now I have been doing it like this.
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(m_targetFilePath, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("client");
xmlWriter.Close();
xmlDoc.Load(m_targetFilePath);
XmlElement root = xmlDoc.DocumentElement;
Saving the doc, then reloading it to get hold of the start element so i can write attributes to it. Does anybody know the correct way of doing this because I'm pretty sure what I'm doing isn't right.
I tried to use xmlWriter.AppendChild() but it doesnt seem to write out anything. :(
If you are using 3.5 or higher, XDocument would make you fall in love.
Have you tried something like this ?
// add the root node
xmlWriter.WriteStartElement("client");
// add the attribute to root node
xmlWriter.WriteStartAttribute("foo");
// add the value of the attribute
xmlWriter.WriteValue("attribute value...");
// close the attribute to root node
xmlWriter.WriteEndAttribute();
// close the root node
xmlWriter.WriteEndElement();
Have you looked at using XmlSerializer? Create a class to hold all your data, create an instance of your class and then use XmlSerializer to write it out to an XML file.

How to Programatically read the Documentation section of a WSDL in C#

i am using a WSDL file to create a the proxy class file, this service has a big Enumeration. the description for each enum value is in documentation section, how can i programatically read that section?
A WSDL file is always an XML file, so you can open it and read the elements data. For example, given the eBay Services WSDL file, you can query the documentation of the value COD of the enumeration BuyerPaymentMethodCodeType like this:
XmlDocument wsdlDoc = new XmlDocument();
wsdlDoc.Load(#"D:\temp\eBaySvc.wsdl");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(wsdlDoc.NameTable);
nsMgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
XmlNode node = wsdlDoc.SelectSingleNode("//xs:simpleType[#name='BuyerPaymentMethodCodeType']/xs:restriction/xs:enumeration[#value='COD']/xs:annotation/xs:documentation", nsMgr);
string description = node.InnerText;

Categories

Resources