I tried to make dynamic Site Map using Asp.net. I'm using XML file to write in which is
<?xml version="1.0" encoding="utf-8"?>
<urlset>
</urlset>
and code behind for Article.aspx which have urls
string xmlpath = #"~/data.xml";
var path = Server.MapPath(xmlpath);
XDocument doc = XDocument.Load(path);
XElement root = new XElement("url");
root.Add(new XElement("loc", url));
root.Add(new XElement("lastmod", DateTime.Now.ToString("yyyy-MMdd")));
root.Add(new XElement("lastmod", "daily"));
root.Add(new XElement("priority", "1.0"));
doc.Element("urlset").Add(root);
doc.Save(path);
This code is working good and I get the XML file correctly the problem is Google Search console need me to add xmlns nameSpace to the urlset element so it must be like this:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
</urlset>
When I do add the NameSpace xmlns to the the <urlset> tag I get this error:
object reference not set
I like creating a new XDocument by parsing a string especially when you have namespaces
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" +
"</urlset>";
XDocument doc = XDocument.Parse(xml);
XElement urlset = doc.Root;
I found answer as "madreflection" mentioned at Microsoft community
code behind:
XNamespace aw = "http://www.sitemaps.org/schemas/sitemap/0.9";
doc.Element(aw + "urlset").Add(root);
and my XML file is
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
</url>
</urlset>
Related
I'm trying to get a single value from an XML using the SelectSingleNode, but it keeps returning null on me.
I've been looking here on SO and it seems it has something to do with the namespace. I tried adding it but I keep getting null.
The XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<EML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xnl="urn:oasis:names:tc:ciq:xnl:4"
xmlns:xal="urn:oasis:names:tc:ciq:xal:4"
xmlns="urn:oasis:names:tc:evs:schema:eml"
xmlns:martine="http://www.martine.be/extensions"
Id="510"
SchemaVersion="7.0"
xsi:schemaLocation="urn:oasis:names:tc:evs:schema:eml schema/510-count-v7-0.xsd
http://www.martine.be/extensions schema/martine-eml-extensions.xsd">
<EMLHeader>
<TransactionId>01</TransactionId>
<ManagingAuthority>
<AuthorityIdentifier>2</AuthorityIdentifier>
<AuthorityName>
<NameElement ElementType="">VLR</NameElement>
</AuthorityName>
<Description>Some Description</Description>
<OrganizationURL>Unknown</OrganizationURL>
<AuthorityAddress/>
</ManagingAuthority>
</EMLHeader>
I'm trying to extract the Description using the code below:
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:evs:schema:eml");
XmlNode testNode = doc.SelectSingleNode("/ns:EML/ns:EMLHeader/ns:ManagingAuthority/ns:Description", nsmgr);
if (testNode != null)
{
Console.WriteLine(testNode.InnerText);
}
What am doing wrong?
Tested this and you are missing closing </EML> tag. This was the error I got
Unhandled Exception: System.Xml.XmlException: Unexpected end of file has occurred. The following elements are not closed: EML. Line 24, position 17.
TestCodeApp.cs
using System;
using System.Xml;
public class Program
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:evs:schema:eml");
XmlNode testNode = doc.SelectSingleNode("/ns:EML/ns:EMLHeader/ns:ManagingAuthority/ns:Description", nsmgr);
if (testNode != null)
{
Console.WriteLine(testNode.InnerText);
}
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<EML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xnl="urn:oasis:names:tc:ciq:xnl:4"
xmlns:xal="urn:oasis:names:tc:ciq:xal:4"
xmlns="urn:oasis:names:tc:evs:schema:eml"
xmlns:martine="http://www.martine.be/extensions"
Id="510"
SchemaVersion="7.0"
xsi:schemaLocation="urn:oasis:names:tc:evs:schema:eml schema/510-count-v7-0.xsd
http://www.martine.be/extensions schema/martine-eml-extensions.xsd">
<EMLHeader>
<TransactionId>01</TransactionId>
<ManagingAuthority>
<AuthorityIdentifier>2</AuthorityIdentifier>
<AuthorityName>
<NameElement ElementType="">VLR</NameElement>
</AuthorityName>
<Description>Some Description</Description>
<OrganizationURL>Unknown</OrganizationURL>
<AuthorityAddress/>
</ManagingAuthority>
</EMLHeader>
</EML>
Your code look OK, except:
you missed close node in the xml file: ""
if your content is contain information of above xml, then you can use doc.LoadXML(content), otherwise you should use doc.Load(fileName).
Your xml file should be:
<?xml version="1.0" encoding="UTF-8"?>
<EML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xnl="urn:oasis:names:tc:ciq:xnl:4"
xmlns:xal="urn:oasis:names:tc:ciq:xal:4"
xmlns="urn:oasis:names:tc:evs:schema:eml"
xmlns:martine="http://www.martine.be/extensions"
Id="510"
SchemaVersion="7.0"
xsi:schemaLocation="urn:oasis:names:tc:evs:schema:eml schema/510-count-v7-0.xsd
http://www.martine.be/extensions schema/martine-eml-extensions.xsd">
<EMLHeader>
<TransactionId>01</TransactionId>
<ManagingAuthority>
<AuthorityIdentifier>2</AuthorityIdentifier>
<AuthorityName>
<NameElement ElementType="">VLR</NameElement>
</AuthorityName>
<Description>Some Description</Description>
<OrganizationURL>Unknown</OrganizationURL>
<AuthorityAddress/>
</ManagingAuthority>
</EMLHeader>
</EML>
And then you can read it:
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:evs:schema:eml");
XmlNode testNode = doc.SelectSingleNode("/ns:EML/ns:EMLHeader/ns:ManagingAuthority/ns:Description", nsmgr);
if (testNode != null)
{
Console.WriteLine(testNode.InnerText);
}
I am creating an XML document using the Task Factory XML Output Destination task though the output needs some tweaking and I can't seem to find a way to do this within the task so figured I would be able to do this via the script component with C#.
This is the XML I am working with
<?xml version="1.0" encoding="utf-8"?>
<CSDS xmlns="http://www.datadictionary.nhs.uk/messages/CSDS-v1-0">
<CYP000>
<C000010>1.0</C000010>
</CYP000>
</CSDS>
And I need to change the XML to look like:
<?xml version="1.0" encoding="utf-8"?>
<CSDS:CSDS xmlns:CSDS="http://www.datadictionary.nhs.uk/messages/CSDS-v1-0">
<CYP000>
<C000010>1.0</C000010>
</CYP000>
</CSDS>
I have tried creating a new XDocument declaring the namespace and adding the source elements in but this adds the namespace to all elements.
string xmlFile = #"C:\Temp\csds.xml";
string newXmlFile = #"C:\Temp\csds-new.xml";
XDocument sourceXml = XDocument.Load(xmlFile);
XNamespace ns = "http://www.datadictionary.nhs.uk/messages/CSDS-v1-0";
XDocument newXml = new XDocument(new XDeclaration("1.0", "utf-8", null));
XElement newRoot = new XElement(ns + "CSDS",
new XAttribute(XNamespace.Xmlns + "CSDS", ns.NamespaceName));
newRoot.Add(sourceXml.Root.Elements());
newRoot.Save(newXmlFile);
Output
<?xml version="1.0" encoding="utf-8"?>
<CSDS:CSDS xmlns:CSDS="http://www.datadictionary.nhs.uk/messages/CSDS-v1-0">
<CSDS:CYP000>
<CSDS:C000010>1.0</CSDS:C000010>
</CSDS:CYP000>
</CSDS:CSDS>
I have found that doing the following after loading my file works along with the rest of my code:
foreach (var descendants in sourceXml.Root.Descendants())
descendants.Name = descendants.Name.LocalName;
Example
<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<Tag1>XXXX</Tag1>
<Tag2>YYYY</Tag2>
<Tag3>true</Tag3>
<Tag4>ZZZZ</Tag4>
</Settings>
I want to edit only the contents of Tag3 without having to create another .xml file
You may edit your XML file like this:
XmlDocument doc = new XmlDocument();
doc.Load("D:\\somefile.xml");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("Settings::Tag3");
myNode.Value = "blabla";
doc.Save("D:\\somefile.xml");
I Want add some url to xml file on my website using C#.
I have already create a XML file on website Root.
The content of xml file is :
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
</urlset>
Now I want add new <url> Node with <loc> node to xml file and I want xml content changes like
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
<url>
<loc>http://www.structure.com/Structure.aspx?id=2</loc>
</url>
</urlset>
I try made a function that get url string from web form and it's triggers on asp:Button click
protected void Button1_Click(object sender, EventArgs e)
{
insertSiteMap("http://www.structure.com/Structure.aspx?id=2");
}
And the function is:
private void insertSiteMap(string pageurl)
{
//Load XML Schema
System.Xml.XmlDocument originalXml = new System.Xml.XmlDocument();
originalXml.Load(Server.MapPath("../sitemap.xml"));
XmlElement URL = originalXml.CreateElement("url");
XmlElement LOC = originalXml.CreateElement("loc");
XmlText LOCText = originalXml.CreateTextNode(pageurl);
LOC.AppendChild(LOCText);
URL.AppendChild(LOC);
XmlNode newUrl = originalXml.GetElementsByTagName("url")[0];
originalXml.DocumentElement.PrependChild(newUrl);
originalXml.Save(Server.MapPath("../sitemap.xml"));
}
I don't have any error and visual studio message me xml file has been modified but when I open the file there is no any changes on xml file :(.
am I did wrong any where?
Once you have created the new element with your variable URL, you need to insert with e.g. originalXml.DocumentElement.AppendChild(URL);.
However, take note that in your original XML you use a namespace while your C# code creates the new XmlElements in no namespace, so you need to fix the element creation as well, e.g.
XmlElement url = originalXml.CreateElement("url", originalXml.DocumentElement.NamespaceURI);
XmlElement loc = originalXml.CreateElement("loc", originalXml.DocumentElement.NamespaceURI);
loc.InnerText = pageurl;
url.AppendChild(loc);
originalXml.DocumentElement.AppendChild(url);
i have an xml file sitemap.xml as shown below ..i need to add one more
tag here after <Name> tag..Means After <Name>test</Name> tag here
i need to add destination tag like <Destination>NY</Destination>
.Can we add contents to xml through a textbox by pressing a button
control without manually doing
this is xml file sitemap.xml
<?xml version="1.0" encoding="utf-8" ?>
<ObjectClass>
<Image>00000000-0000-0000-0000-000000000000</Image>
<Description />
<Name>test</Name>
<DefaultApp>00000000-0000-0000-0000-000000000000</DefaultApp>
<ID>464930eb-e518-4d0c-b80b-184c97c7dd27</ID>
<ParentClassID>00000000-0000-0000-0000-000000000002</ParentClassID>
<DynamicPopulation>false</DynamicPopulation>
<TimeoutPeriod>0</TimeoutPeriod>
<Persist>false</Persist>
<ClassVersion>1</ClassVersion>
<Reinitialize>false</Reinitialize>
</ObjectClass>
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlElement elt = doc.CreateElement("Destination");
elt.InnerText = "NY";
doc.DocumentElement.AppendChild(elt);
doc.Save(fileName);
To delete an element :
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement elt = doc.DocumentElement.SelectSingleNode("Destination") as XmlElement;
if (elt != null)
doc.DocumentElement.RemoveChild(elt);
doc.Save();