I have a Xml file:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:inf="http://www.granfol.com/luna" >
<Employees>
<Employee Department="e" kk="TEST11111111">
<Name>TestName11111</Name>
</Employee>
</Employees>
</ResourceDictionary>
and I want to set the xmlns to
xmlns=""
or
Directly Remove the xmlns default namespace.
I have tried
XElement X = XElement.Load(#"FILEPATH");
IEnumerable<XAttribute> XI = X.Attributes();
foreach (XAttribute x in XI)
Console.WriteLine(x);//Show all namespace in root
X.SetAttributeValue("xmlns","");
foreach (XAttribute x in XI)
Console.WriteLine(x); //Here shows the namespace xmlns has change to ""
X.Save(#"FILEPATH"); //But when save File it throws Exception and failed
When save, it throws exception like "The prefix cannot be redefined from to within the same start element tag"
I also tried to use
X.Attributes("xmlns").Remove();
to replace
X.SetAttributeValue("xmlns","");
above,
It doesn't throw exception when save,
but when I open the xml file, I see the "xmlns" doesn't disappear..
then I test and find
X.Attributes("xmlns")
doesn't contain xmlns, but contain "xmlns:x, xmlns:sys, xmlns:inf"
So is there some way to set value or remove xmlns namespace in Xml ?
I have thought a way is that to reproduce the whole xml file again,
but I'm not sure it's easy to do if the xml file is large.
I don't really know how to do it, but suppose it's not a good idea.
Related
Following is the XML format:
<?xml version="1.0" encoding="UTF-8"?>
<package version="2.0" unique-identifier="isbn0000000000000" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>Eltern Family</dc:title>
<dc:creator></dc:creator>
<dc:publisher></dc:publisher>
<dc:rights></dc:rights>
<dc:identifier id="isbn0000000000000">0000000000000</dc:identifier>
<dc:language>de</dc:language>
<dc:date opf:event="publication">2019-02-11</dc:date>
</metadata>
</package>
Here I got the default Namespace by XDocument.Root.GetDefaultNamespace();. But as you can see, there are multiple namespaces in the <metadata> XML node. The problem is that, they are variable i.e., each XML may have different values, so I cannot declare a variable with one fixed value.
How do I get the namespaces, so that I can add values to the descendant elements?
Please help.
Regards
Aman
If, as you say, you want to set the content of dc:rights, then you need to get hold of that element.
You can do this by name - the 'qualified name' is made of of the namespace and a local name. The namespace prefix dc is not actually important in and of itself, it's just used as a shorthand to refer to the namespace within the document.
Assuming you have parsed this XML to an XDocument called doc:
XNamespace dc = "http://purl.org/dc/elements/1.1/"
var rights = doc.Descendants(dc + "rights").Single();
rights.Value = "text";
I'm trying to create some xml nodes runtime using XPath for C#. See XML Below:
<Package xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest" xmlns="http://schemas.microsoft.com/appx/2010/manifest">
<Application>
<m2:VisualElements>
<!--- INSERT CHILD NODES HERE WHICH ALSO HAVE NAMESPACE 'm2' ---->
</m2:VisualElements>
</Application>
</Package>
Currently I'm doing the following:
XElement visualElements = doc.Descendants().SingleOrDefault(p => p.Name.LocalName == "VisualElements");
visualElements.Add(new XElement(doc.Root.GetDefaultNamespace() + "InitialRotationPreference"));
I know that this is wrong since I reference the default namespace, this will result in this being added:
<InitialRotationPreference />
When I want:
<m2:InitialRotationPreference />
Is there some way to access the parent-nodes namespace (m2) without "knowing" the prefix or the namespace-url?
Thank you!
Your document root's namespace is http://schemas.microsoft.com/appx/2010/manifest. Use the one from VisualElements:
XName name = visualElements.Name.Namespace + "InitialRotationPreference"
Or specify explicitly:
XName name = XName.Get("InitialRotationPreference",
"http://schemas.microsoft.com/appx/2013/manifest");
Then add an element with that name:
visualElements.Add(new XElement(name));
I want to generate below code snippet into xml with c#:
<?xml version="1.0" encoding="utf-8" ?>
<PrincetonStorageRequest
xmlns="http://munichre.com/eai/dss/pct/v1.0"
requestId="RequestOut_MAG_Test_02"
timestampUtc="2015-02-19T09:25:30.7138903Z">
<StorageItems>
and my code is :
XmlWriter writer = XmlWriter.Create(fileName);
writer.WriteStartDocument(true);
writer.WriteStartElement("PrincetonStorageRequest");
writer.WriteAttributeString("xmlns","http://example.com/abc/dss/pct/v1.0");
writer.WriteAttributeString("requestId",name);
writer.WriteAttributeString("timestampUtc","2015-02-19T09:25:30.7138903Z");
writer.WriteStartElement("StorageItems");
But I am getting
"The prefix " cannot be redefined from " to within the same start element tag.
From your XML and the error, I believe it's because you are adding a default namespace after adding an element with no namespace declaration, so you're effectively creating an element and then changing its namespace.
Try the following code - it stops the error when I test it locally just for the XML I think you're trying to get:
XmlWriter writer = XmlWriter.Create(fileName);
writer.WriteStartDocument(true);
writer.WriteStartElement("PrincetonStorageRequest", "http://example.com/abc/dss/pct/v1.0");
writer.WriteAttributeString("xmlns", "http://example.com/abc/dss/pct/v1.0");
writer.WriteAttributeString("requestId", name);
writer.WriteAttributeString("timestampUtc", "2015-02-19T09:25:30.7138903Z");
writer.WriteStartElement("StorageItems");
So when I create the PrincetonStorageRequest element I am specifying a namespace URI.
Edit: Just to check, this is the XML that gets created but I did have to add the code to write the end elements:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<PrincetonStorageRequest xmlns="http://example.com/abc/dss/pct/v1.0" requestId="RequestOut_MAG_Test_02" timestampUtc="2015-02-19T09:25:30.7138903Z">
<StorageItems/>
I am trying to read the entitysets within the EDMX file from Entity Framework.
The EDMX file (XML format) has the following layout:
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<edmx:Runtime>
<edmx:ConceptualModels>
<Schema Namespace="Model" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="EntityModel" p1:LazyLoadingEnabled="true">
<EntitySet Name="TableName" EntityType="Model.TableName" />
I am using following XPath to get all EntitySet Nodes within the EntityContainer:
/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/Schema/EntityContainer/EntitySet
but I am getting no result with this C# code:
XmlDocument xdoc = new XmlDocument("pathtoedmx");
var ns = new XmlNamespaceManager(xdoc.NameTable);
ns.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2009/11/edmx");
ns.AddNamespace("annotation", "http://schemas.microsoft.com/ado/2009/02/edm/annotation");
ns.AddNamespace("p1", "http://schemas.microsoft.com/ado/2009/02/edm/annotation");
ns.AddNamespace("", "http://schemas.microsoft.com/ado/2009/11/edm");
var entitySets = xdoc.SelectNodes("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/Schema/EntityContainer/EntitySet", ns);
Already got the XPath from this tool (http://qutoric.com/xmlquire/), because I started not trusting my own XPath skills but it tells me the same XPath I was already using.
If I remove the "/Schema/EntityContainer/EntitySet" part its finding the "/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels", but not further on already tried to specify the "edmx" namespace ("edmx:/Schema") but no difference.
Hope you can help me out, already banging my head against the table. :)
Namespaces are a convention on how to combine two different XML dialects into a single document. Those prefixes really doesn't matter as long you keep your URI component exactly the same. For instance, take something like this:
ns.AddNamespace("xxx", "http://schemas.microsoft.com/ado/2009/11/edmx");
Console.WriteLine(xdoc.SelectNodes("/xxx:Edmx", ns).Count); // 1
You'll get one node because your namespace URI matched, despite your "wrong" namespace prefix.
If you have an attribute named xmlns, current element and it's children will inherits that namespace URI.
In your case, your root element doesn't have a default namespace and that's ok. But your Schemas element does have a namespace and you need to inform it. I came with this code:
// change "" to "edm"
ns.AddNamespace("edm", "http://schemas.microsoft.com/ado/2009/11/edm");
var entitySets = xdoc.SelectNodes("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityContainer/edm:EntitySet", ns);
I have the following line of code defined in codebehind in silverlight:
Path path = XamlReader.Load( "<Path Data=\"F1 M 44.207,34.0669C 44.4841,33.7278 44.7612,33.3886 45.0383,33.0494\" />" ) as Path;
No idea why it's happening...
As the exception states, you are missing the default namespace in your XAML document. The <Path> element needs an XML namespace.
Add the attribute xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" to your Path element.
http://msdn.microsoft.com/en-us/library/ms747086.aspx
And for reference, in case you're not familiar with them, as XAML is just XML, here is an introduction to XML namespaces:
http://www.w3schools.com/xml/xml_namespaces.asp