I need to generate XML in the following form:
<ns1:root xmlns:ns1="http://example.com/xmlns1" xmlns:ns2="http://example.com/xmlns2">
<ns2:item ns2:param="value" />
</ns1:root>
I use this code:
XmlDocument xDoc = new XmlDocument();
XmlElement xRootElement = (XmlElement)xDoc.AppendChild(xDoc.CreateElement("ns1:root"));
xRootElement.SetAttribute("xmlns:ns1", "http://example.com/xmlns1");
xRootElement.SetAttribute("xmlns:ns2", "http://example.com/xmlns2");
XmlElement xElement = (XmlElement)xRootElement.AppendChild(xDoc.CreateElement("ns2:item"));
xElement.SetAttribute("ns2:param", "value");
But the result is the following:
<root xmlns:ns1="http://example.com/xmlns1" xmlns:ns2="http://example.com/xmlns2">
<item param="value" />
</root>
Thanks for the advice.
XmlElement xElement = (XmlElement)xRootElement.AppendChild(xDoc.CreateElement("ns2:item", "http://example.com/xmlns2"));
Gave me:
<root xmlns:ns1="http://example.com/xmlns1" xmlns:ns2="http://example.com/xmlns2">
<ns2:item param="value" />
</root>
By the way, "ns2:param" is not necessary. XML attributes belong to the same namespace as the element.
Try this:
XmlDocument xDoc = new XmlDocument();
XmlElement xRootElement = (XmlElement)xDoc.AppendChild(xDoc.CreateElement("ns1:root"));
xRootElement.SetAttribute("xmlns:ns1", "http://example.com/xmlns1");
xRootElement.SetAttribute("xmlns:ns2", "http://example.com/xmlns2");
XmlElement xElement = (XmlElement)xRootElement.AppendChild(xDoc.CreateElement("item", "http://example.com/xmlns1"));
xElement.SetAttribute("param", "http://example.com/xmlns1", "value");
Related
I can't figure out what namespaces need to be used for following Xml (sample):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ResponseCharge xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ResponseInfo xmlns="http://www.example.net">
<ResponseDetail>
<ResponseCode>00</ResponseCode>
<RequestIdentifier>1029034</RequestIdentifier>
<RetrievalReferenceNumber>634716660366</RetrievalReferenceNumber>
<ProcessingTime>00:03:314</ProcessingTime>
</ResponseDetail>
<ResponseTransactionDetail>
<AVSResponseCode />
<CVVResponseCode />
<AuthorizationSourceCode />
<AuthorizationNumber>004454</AuthorizationNumber>
</ResponseTransactionDetail>
<EMVResponseData />
</ResponseInfo>
</ResponseCharge>
</s:Body>
</s:Envelope>
My code looks like this atm:
XmlDocument doc = new XmlDocument();
doc.Load(responseXmlFile);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
//nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
//nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
//nsmgr.AddNamespace("", "http://www.example.net");
// getting NULL here
XmlNode node = doc.SelectSingleNode("/s:Envelope/s:Body/ResponseCharge/ResponseInfo/ResponseTransactionDetail/AuthorizationNumber", nsmgr);
I'm following this post: Namespace Manager or XsltContext needed
You need to include the namespace in your query for the elements in the http://www.example.net/ namespace.
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ex", "http://www.example.net");
XmlNode node = doc.SelectSingleNode(
"/s:Envelope/s:Body/ResponseCharge/ex:ResponseInfo/ex:ResponseTransactionDetail/ex:AuthorizationNumber",
nsmgr);
I've arbitrarily picked ex as the prefix here. What's defined in the XmlNamespaceManager only relates to your XPath query, the prefixes used in the document aren't relevant.
Alternatively, I'd suggest you use LINQ to XML:
XNamespace ns = "http://www.example.net"
var doc = XDocument.Load(responseXmlFile);
var authNo = (int)doc.Descendants(ns + "AuthorizationNumber").Single();
I'm trying to change a the value for the model atribute from the following .xml file.
<MTConnectDevices xmlns="urn:mtconnect.org:MTConnectDevices:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1 http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
<Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="2" bufferSize="131072" />
<Devices>
<Device name="XX" iso841Class="1" uuid="000" id="id1001">
<Description manufacturer="name" serialNumber="Serial Number" model="UNDEFINED">
</Description>
This is what I've tried but it's not working as it's not changing the value for model.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(mypath);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Header/ns:Devices/ns:Device/ns:Description", nsmgr);
node.Attributes["model"].Value = "changed";
xmlDoc.Save(mypath);
Can anyone give me a hand and tell me how to fix this?
Your xml contains namespaces, so you have to use them while retrieving node.
It can be done something like this:
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Devices/ns:Device/ns:Description", nsmgr);
I would like to read a XML File with C#, but i always get an error.
This is my XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<OMDS xmlns="urn:omds20" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:omds20 ./omds26-00.xsd">
<PAKET VUNr="014" PaketZpktErstell="2014-08-29T10:45:08.575" PaketZpktLetztErstell="2014-08-29T10:45:08.575" PaketInhCd="VM" PaketUmfCd="G" VUVersion="26-00" DVRNrAbs="0">
<PERSON ....
<PERSON ....
<PERSON ....
I would like to read this XML, but XMLContentNodes is always null. So i am unable to get the SelectSingleNode with this Path, but i cant find out what should be wrong here?
XmlDocument doc = new XmlDocument();
doc.Load(openFileDialog1.FileName);
XmlNode XMLContentNodes = doc.SelectSingleNode("/OMDS/PAKET"); // Error Here
XmlNodeList PersonNodeList = XMLContentNodes.SelectNodes("PERSON");
foreach (XmlNode node in PersonNodeList)
{
.....
Any help would be greatly appreciated.
The usual namespace problem. Try
XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
mgr.AddNamespace("d", "urn:omds20");
XmlNode XMLContentNodes = doc.SelectSingleNode("/d:OMDS/d:PAKET", mgr);
XmlNodeList PersonNodeList = XMLContentNodes.SelectNodes("d:PERSON", mgr);
You'll need to add the namespace urn:omds20 to the doc XmlDocument object after loading your XML file in it. It will look like the following:
XmlDocument doc = new XmlDocument();
doc.Load(openFileDialog1.FileName);
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("omds20", "urn:omds20");
Then you can query for the PAKET node like this:
XmlNode paketNode = doc.SelectSingleNode("/omds20:OMDS/omds20:PAKET", xmlnsManager);
I am having an XML file. I would like to create a new node and append it at the end of the XML file and save it back to memory.
<IntCal>
<User>
<Date>12/09/2012</Date>
<Client>abcd</Client>
<Jewellery>Others</Jewellery>
<ROI>7.5</ROI>
<Description>Some Description</Description>
</User>
<IntCal>
I would like to create a new <User> element. Any idea how to do it.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode root_node;
//XmlNodeList nodeList = xmlDoc.SelectNodes("/IntCal/User");
XmlDocument new_node = new XmlDocument();
root_node = xmlDoc.CreateElement("IntCal");
xmlDoc.AppendChild(root_node);
Thanks
Reference: http://msdn.microsoft.com/en-us/library/fw1ys7w6(v=vs.100).aspx
XmlElement elem = xmlDoc.CreateElement("User");
xmlDoc.DocumentElement.AppendChild(elem);
If you want to go the LINQ route, you could do:
XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Element("IntCal")
.Add(new XElement("User"));
Personally, I'd opt for the XDocument and use LINQ but either way works.
Reference: http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
XmlDocument is an old school, why don't you use XDocument, simple and easy:
XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Root.Add(new XElement("User",
new XElement("Client", "John"),
new XElement("Jewellery", "Others")));
xDoc.Save(xmlFile);
References:
XDocument or XmlDocument
Performance: XDocument versus XmlDocument
The problem is the ServiceDocument as the xmlns attribute on it.
---Preassigned XML
System.Xml.XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ServiceDocument
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"
>
<BaseUri>
http://xxxx.xxxxx.net/xxx.1/
</BaseUri>
<ProfilesLink>
http://adf.apis.dds.net/af.1/
</ProfilesLink>
<SignedInUser>
<Cid>
4433sfsdfgd
</Cid>
<Uri>
http://fd.apis.afdafd.net/V4.1/cid-xxxxx/adad
</Uri>
</SignedInUser>
<StatusMessageLink>
http://psm.adfa.afd.net/dfa.1/
</StatusMessageLink>
</ServiceDocument>"
);
// Response.Write(xmlDoc.InnerXml);
--//PARSE XML problem is below**
Response.Write(xmlDoc.SelectSingleNode("/ServiceDocument/BaseUri").InnerXml);
You need to assign short aliases to the namespaces using the XMLNamespaceManager.
See this page for an example.
So, to solve your problem:
var xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNamespaceManager.AddNamespace("ds", "http://schemas.microsoft.com/ado/2007/08/dataservices");
var result = xmlDoc.SelectNodes("/ds:ServiceDocument/ds:BaseUri", xmlNamespaceManager);