how to add xmlns attribute to an existing xml document - c#

I have existing xml document.
e.g
<Test>
<A />
</Test>
I load this xml into XDocument. I need to add attribute xmlns to this document and save it with this attribute.
var xml = new XDocument.Load("c:\\filePath.xml");
When I'm trying this:
xml.Root.SetAttributeValue("xmlns", "http://namespaceuri");
I'm getting exception:
System.Xml.XmlException: The prefix '' cannot be redefined from 'http://namespaceuri' to within the same start element tag.
thanks

You need to set the names to be in the namespace as well:
XNamespace ns = "http://namespaceuri";
foreach (var element in xml.Descendants().ToList())
{
element.Name = ns + element.Name.LocalName;
}
xml.Root.SetAttributeValue("xmlns", ns.ToString());
Basically you're trying to move all the elements to that namespace and make it the default namespace for the root element down. You can't change the default namespace while leaving the element itself in a different-but-unqualified namespace.
Using the code above with your sample XML (fixed to close A) ends up with:
<Test xmlns="http://namespaceuri">
<A />
</Test>
Note that this code will change the namespace of all elements. If you want to be more selective, you should add a Where call after the xml.Descendants() call, e.g.
foreach (var element in xml.Descendants()
.Where(x => x.Name.Namespace == XNamespace.None)
.ToList())

Related

Delete Attribute of Xml node c#

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.

How to read colon in xml file using c# linq

How can I read colon in xml file using c# linq
My code in c#
XDocument xdocc = XDocument.Load(files, LoadOptions.SetLineInfo);
var mml_hi_it = xdocc.Descendants("mml:hi").Where(s => (string)s.Attribute("rend").Value == "it");
The mml literal indicates the namespace used in your xml. You should be able to find the definition in the root node. For example, based on your comment, your root tag looks like following.
<article article-type="research-article" dtd-version="1.1" xml:lang="en" xmlns:mml="w3.org/1998/Math/MathML" xmlns:xlink="w3.org/1999/xlink" xmlns:oasis="niso.org/standards/z39-96/ns/oasis-exchange/table">
In order to access Element with namespace, you need to specify the namespace using XNamespace. For example,
XNamespace mml = "w3.org/1998/Math/MathML";
var mml_hi_it = xdocc.Descendants(mml + "hi").Where(s => (string)s.Attribute("rend").Value == "it");

Removing header in xml file required?

I have the xml file below and I want to retrieve the sym attribute from the Instrmt element using LINQ to XML. In order to do that, do I have to remove the first line: FIXML element?
If I do that then the code below works:
IEnumerable<string> symbols = from c in xml.Descendants("Instrmt")
select (string)c.Attribute("Sym");
Here is the xml.
<FIXML r="20030618" s="20040109" v="4.4" xr="FIA" xv="1" xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.fixprotocol.org/FIXML-4-4 https://optionsclearing.com/components/docs/membership/dds_ref/fia_1_1/fixml-main-4-4-FIA-1-1.xsd">
<Batch>
<SecList ListTyp="109" ListID="20175" BizDt="2017-12-07">
<SecL Ccy="USD">
<Instrmt Desc="iShares S&P 100 ETF" SecTyp="OPT" SubTyp="ETO" Sym="OEF" Mult="100.0">
<AID AltID="00013" AltIDSrc="RBHP"/>
</Instrmt>
<InstrmtExt>
<Attrb Typ="101" Val="1.0000"/>
<Attrb Typ="108" Val="1.0000"/>
</InstrmtExt>
<Undly Desc="iShares S&P 100 ETF" Px="117.110000" Ccy="USD" Sym="OEF" ID="464287101" Src="1"/>
<Stip Typ="RBHMIN" Val="2.500"/>
<Stip Typ="CPMMIN" Val="3.750"/>
</SecL>
</SecList>
</Batch>
</FIXML>
Ok, your problem is not about the header, it's about the namespace (defined by the xmlns attribute). As your xml define one, all your xml children elements will have it as their default. Try the following code:
var xml = XDocument.Load(pathToFile);
XNamespace ns = xml.Root.GetDefaultNamespace();
IEnumerable<string> symbols = from c in xml.Descendants(ns + "Instrmt")
select (string)c.Attribute("Sym");

How to remove xmlns attribute of a node other than root in an XDocument?

Situation
I'm using XDocument to try and remove an xmlns="" attribute on the first inner node:
<Root xmlns="http://my.namespace">
<Firstelement xmlns="">
<RestOfTheDocument />
</Firstelement>
</Root>
So what I want as a result is:
<Root xmlns="http://my.namespace">
<Firstelement>
<RestOfTheDocument />
</Firstelement>
</Root>
Code
doc = XDocument.Load(XmlReader.Create(inStream));
XElement inner = doc.XPathSelectElement("/*/*[1]");
if (inner != null)
{
inner.Attribute("xmlns").Remove();
}
MemoryStream outStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(outStream);
doc.Save(writer); // <--- Exception occurs here
Problem
Upon trying to save the document, I get the following exception:
The prefix '' cannot be redefined from '' to 'http://my.namespace' within the same start element tag.
What does this even mean and what can I do to remove that pesky xmlns=""?
Notes
I do want to keep the root node's namespace
I only want that specific xmlns removed, there will be no other xmlns attributes in the document.
Update
I've tried using code inspired from answers on this question:
inner = new XElement(inner.Name.LocalName, inner.Elements());
When debugging, the xmlns attribute is gone from it but I get the same exception.
I think the code below is what you want. You need to put each element into the right namespace, and remove any xmlns='' attributes for the affected elements. The latter part is required as otherwise LINQ to XML basically tries to leave you with an element of
<!-- This would be invalid -->
<Firstelement xmlns="" xmlns="http://my.namespace">
Here's the code:
using System;
using System.Xml.Linq;
class Test
{
static void Main()
{
XDocument doc = XDocument.Load("test.xml");
// All elements with an empty namespace...
foreach (var node in doc.Root.Descendants()
.Where(n => n.Name.NamespaceName == ""))
{
// Remove the xmlns='' attribute. Note the use of
// Attributes rather than Attribute, in case the
// attribute doesn't exist (which it might not if we'd
// created the document "manually" instead of loading
// it from a file.)
node.Attributes("xmlns").Remove();
// Inherit the parent namespace instead
node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
}
Console.WriteLine(doc); // Or doc.Save(...)
}
}
There is no need to 'remove' the empty xmlns attribute. The whole reason that the empty xmlns attrib is added is because the namespace of your childnodes is empty (= '') and therefore differ from your root node. Adding the same namespace to your childs as well will solve this 'side-effect'.
XNamespace xmlns = XNamespace.Get("http://my.namespace");
// wrong
var doc = new XElement(xmlns + "Root", new XElement("Firstelement"));
// gives:
<Root xmlns="http://my.namespace">
<Firstelement xmlns="" />
</Root>
// right
var doc = new XElement(xmlns + "Root", new XElement(xmlns + "Firstelement"));
// gives:
<Root xmlns="http://my.namespace">
<Firstelement />
</Root>

Reading XML with XDocument problem

I have a following XML (part of a .rdl report):
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition">
<DataSources>
<DataSource Name="TMSSharepointDataSource">
<DataSourceReference>TMSSharepointDataSource</DataSourceReference>
<rd:DataSourceID>f06ffa33-238f-4d83-adfe-1eaa8df96e90</rd:DataSourceID>
</DataSource>
</DataSources>
</Report>
I try to parse and read it using following code:
byte[] fileContent = File.ReadAllBytes(#"path");
UTF8Encoding unicode = new UTF8Encoding();
string stringContent = unicode.GetString(fileContent);
XDocument xml = XDocument.Parse(stringContent);
XElement dsNode = xml.Root.Element("DataSources");
I can't figure out why is dsNode always null?
It's a namespace issue... you need to specify the namespace for the DataSources element. Fortunately, LINQ to XML makes this really easy:
XNamespace ns = "http://schemas.microsoft.com/sqlserver/" +
"reporting/2008/01/reportdefinition";
XElement dsNode = xml.Root.Element(ns + "DataSources");
Note the xmlns="http://..." part of the root element, which means that element and all elements below it which don't have an explicit namespace inherit that namespace.
You are probably missing a namespace reference. Your DataSources will inherit the namespace of the Report node and you will need both the namespace and element local name to generate an XName.
Alternatively you can do the following and skip the namespace check:
XElement dsNode =
xml
.Root
.DescendantNodes()
.Where(e => e.Name.LocalName.Equals("DataSources"))
.First();
This will return the first node where the local name is DataSources. In your example this will be the DataSources element.
Also, your loading of the document is very clumsy. I would suggest the following instead:
XDocument xml = XDocument.Load(File.OpenRead(#"path"));

Categories