I receive an XML file which has on the root node a xmlns namespace assigned:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message xmlns="http://www.myAddress.com/DataRequest/message/">
<Date>2017/01/01</Date>
</Message>
I do not know how to retrive the Date element using XPath, I tried
var root = xDocument.Root;
var dateElement = root.XPathSelectElement("/Message/Date");
If I remove the namespace from the root xml, then I can retrieve the value using "/Message/Date".
I tried to add xmlns to a XmlNamespaceManager, but I get this error:
Prefix "xmlns" is reserved for use by XML.
How can I get the value?
You should use namespace when you specify element's name. Default namespace is easy to get with XElement.GetDefaultNamespace() method:
var ns = root.GetDefaultNamespace();
var dateElement = (DateTime)root.Element(ns + "Date");
If you want to use XPath:
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
manager.AddNamespace("ns", root.GetDefaultNamespace().ToString());
var dateElement = (DateTime)root.XPathSelectElement("/ns:Message/ns:Date", manager);
I would suggest using LINQ.
Here is a link to the code example:https://msdn.microsoft.com/en-us/library/mt693115.aspx
here is the code:
XElement root = XElement.Load("Message.xml");
IEnumerable<XElement> dateNode=
from el in root.Elements("Date")
select el;
foreach (XElement el in dateNode)
Console.WriteLine(el);
Related
I have a XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ua="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd">
<ServerConfiguration>
<SecurityPolicies>
<ServerSecurityPolicy>
<SecurityMode>None_1</SecurityMode>
</ServerSecurityPolicy>
</SecurityPolicies>
</ServerConfiguration>
</ApplicationConfiguration>
What I want is to add more node named ServerSecurityPolicy by code.
Then I use this code:
string docaddress = "D:\\abc.xml";
XDocument doc = XDocument.Load(docaddress);
var root = doc.Root;
var these = root.Descendants().Where(p => p.Name.LocalName == "SecurityPolicies");
XElement addelement = new XElement("ServerSecurityPolicy");
addelement.Add(new XElement("SecurityMode", "None_1"));
foreach (var elem in these)
{
elem.Add(addelement);
}
doc.Save(docaddress);
It actually works, but the newly added node is something like this:
<ServerSecurityPolicy xmlns="">
<SecurityMode>None_1</SecurityMode>
</ServerSecurityPolicy>
I don't want the attribute "xmlns", then I try to delete it by something like this:
var these2 = root.Descendants().Where(p => p.Name.LocalName == "ServerSecurityPolicy");
foreach (var elem in these2)
{
label2.Text += elem.Attribute("xmlns").Name.LocalName;
elem.Attribute("xmlns").Remove();
}
The label2.Text shows "xmlnsxmlnsxmlsn..." so that I think the elem.Attribute("xmlns") has pointed to the attributes I want to delete, but the Remove() not work.
Can you suggest me some ways to delete the attribute or add node without attribute?
Thank you
The reason you're getting an empty attribute - which xmlns="" refers to - is because your document's root node belongs to the namespace xsi. When you're adding a node without a namespace - as you're doing in your example - you're not actually binding it to the xsi namespace.
To make your node part of the root namespace, replace
new XElement("ServerSecurityPolicy")
with
new XElement("ServerSecurityPolicy",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"))
The problem is that the element you want to create is actually in the http://opcfoundation.org/UA/SDK/Configuration.xsd namespace, which is the default for the document because of this part of the root element:
xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd"
You can create an element in that namespace easily:
XNamespace configNs = "http://opcfoundation.org/UA/SDK/Configuration.xsd";
var these = root.Descendants(configNs + "ServerSecurityPolicy");
When you add that to your document, you'll find it won't add the xmlns="" part, which was present because you were adding an element without a namespace.
I'd also suggest using the namespace to find elements too:
XNamespace configNs = "http://opcfoundation.org/UA/SDK/Configuration.xsd";
var these = root.Descendants(configNs + "SecurityPolicies");
That's much cleaner than just using the local name, in my view.
I am trying to get values from xml respone :
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://adaddaasd.com">
<A>14</A>
<B>Failed</B>
<C>22</C>
</Response>
My code is :
string responseString = await response.Content.ReadAsStringAsync();
var xDocument = XDocument.Parse(responseString);
var responseNode = xDocument.XPathSelectElement("/Response");
var A = xDocument.XPathSelectElement("/Response/A");
But I am getting null values for A and responseNode. Whats wrong? Thanks
You're blatantly ignoring the XML namespace that's defined in your XML document:
<Response xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://adaddaasd.com'>
****************************
You need to include that into your querying - I would try to do it like this:
var xDocument = XDocument.Parse(responseString);
// *define* your XML namespace!
XNamespace ns = "http://adaddaasd.com";
// get all the <Response> nodes under the root with that XML namespace
var responseNode = xDocument.Descendants(ns + "Response");
// from the first <Response> node - get the descendant <A> nodes
var A = responseNode.FirstOrDefault()?.Descendants(ns + "A");
If you insist on using the XPathSelectElement method, then you must define an XmlNamespaceManager and use it in your XPath select:
// define your XML namespaces
XmlNamespaceManager xmlnsmgr = new XmlNamespaceManager(new NameTable());
xmlnsmgr.AddNamespace("ns", "http://adaddaasd.com");
// use the defined XML namespace prefix in your XPath select
var A = xDocument.XPathSelectElement("/ns:Response/ns:A", xmlnsmgr);
I am extracting an element from an xml document but it's returning null
<?xml version="1.0" encoding="utf-8"?>
<Test1
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-test"
xmlns="https://www.google.com/partner/testt.xsd">
<Test2>OK</Test2>
<Test3>1439379003</Test3>
</Test1>
I'm trying to extract test2 element but its returning null
var responseXdoc = XDocument.Parse(response);
var statusElement = responseXdoc.XPathSelectElement("/Test1/Test2");
result statusElement as null but I'm expecting Ok
Problem in Namespace
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://www.google.com/partner/testt.xsd (Its my guess)
Your XML has default namespace which elements in the scope inherit implicitly. To reference elements in namespace using XPath, you need to use namespace prefix which, you need to register before in an XmlNamespaceManager :
var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "https://www.insuranceleads.com/partner/PricePresentationResult.xsd");
var statusElement = responseXdoc.XPathSelectElement("/d:Test1/d:Test2", nsManager);
dotnetfiddle demo
Alternatively, you can use XNamespace and LINQ API to do the same, for example :
XNamespace d = "https://www.insuranceleads.com/partner/PricePresentationResult.xsd";
var statusElement = responseXdoc.Element(d + "Test1").Element(d + "Test2");
I have an XML file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.someurl.com/somefile.xslt"?>
<AutoInsuranceClaim xmlns="http://www.someurl.com/schemas/AutoInsuranceClaim">
<Identification>
<BaseOwner>3</BaseOwner>
<BaseType>ABC123</BaseType>
<BaseTypeRef>471038341757</BaseTypeRef>
</Identification>
</AutoInsuranceClaim>
and I'm trying to read the Identification node. Here's my code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"..\..\Data.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://www.someurl.com/schemas/AutoInsuranceClaim");
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/Identification", nsmgr);
Console.WriteLine("There are {0} nodes...", nodeList.Count);
I know I should get a least 1 value. My understanding of .NET XML parsing is that if you have a default namespace with no prefix, you have to create your own namespace. But this should have returned 1.
If not, what am I missing?
I might be grasping at straws here, but shouldn't you be namespacing both entities in your xpath expression?
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/ns:Identification", nsmgr);
XElement root = XElement.Load("Data.xml");
var identifications = root.Descendants()
.Where(x => x.Name.LocalName == "Identification")
.ToList()
The problem is that you're trying to find an Identification node without a namespace, but it will have defaulted to the same namespace as the parent due to the xmlns=... part. Try this:
var nodeList = xmlDoc.SelectNodes("/ns:AutoInsuranceClaim/ns:Identification",
nsmgr);
Having tried it myself, it printed a count of 1.
Personally I'd use LINQ to XML instead though, which makes namespace easier handling:
XDocument doc = XDocument.Load(#"..\..\Data.xml");
XNamespace ns = "http://www.someurl.com/schemas/AutoInsuranceClaim";
var nodes = doc.Root.Elements(ns + "Identification");
I need to create an attribute "abc" with the prefix "xx" for an element "aaa". The following code adds the prefix but it also adds the namespaceUri to the element.
Required Output:
<mybody>
<aaa xx:abc="ddd"/>
<mybody/>
My Code:
XmlNode node = doc.SelectSingleNode("//mybody");
XmlElement ele = doc.CreateElement("aaa");
XmlAttribute newAttribute = doc.CreateAttribute("xx","abc",namespace);
newAttribute.Value = "ddd";
ele.Attributes.Append(newAttribute);
node.InsertBefore(ele, node.LastChild);
The above code generates :
<mybody>
<aaa xx:abc="ddd" xmlns:xx="http://www.w3.org/1999/XSL/Transform"/>
<mybody/>
Desired output is
<mybody>
<aaa xx:abc="ddd"/>
<mybody/>
And the declaration of the "xx" attribute should be done in the root node like :
<ns:somexml xx:xsi="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://x.y.z.com/Protocol/v1.0">
How can if get the output in the deisred format? If the xml is not in this desired format then it cannot be processed anymore..
Can anyone help?
Thanks,
Vicky
I believe it's just a matter of setting the relevant attribute directly on the root node. Here's a sample program:
using System;
using System.Globalization;
using System.Xml;
class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("root");
string ns = "http://sample/namespace";
XmlAttribute nsAttribute = doc.CreateAttribute("xmlns", "xx",
"http://www.w3.org/2000/xmlns/");
nsAttribute.Value = ns;
root.Attributes.Append(nsAttribute);
doc.AppendChild(root);
XmlElement child = doc.CreateElement("child");
root.AppendChild(child);
XmlAttribute newAttribute = doc.CreateAttribute("xx","abc", ns);
newAttribute.Value = "ddd";
child.Attributes.Append(newAttribute);
doc.Save(Console.Out);
}
}
Output:
<?xml version="1.0" encoding="ibm850"?>
<root xmlns:xx="http://sample/namespace">
<child xx:abc="ddd" />
</root>