我编写了一个从excel创建xml文档的程序,但它输出的格式如下(注意关键和值节点上的xmls =""(我不想要)本))
<?xml version="1.0" encoding="utf-8"?>
<Content xmlns="uuid:5c2dffe6-cf72-4211-a8a0-dc6a7baf3ff1">
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key xmlns="">/*$0*/</key>
<value xmlns="">/</value>
</Redirect>
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key xmlns="">/2014-12-22</key>
<value xmlns="">/</value>
</Redirect>
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key xmlns="">/a</key>
<value xmlns="">/</value>
</Redirect>
</Content>
所需格式注意键和值节点没有xmlns =&#34;&#34;
<Content xmlns="uuid:5c2dffe6-cf72-4211-a8a0-dc6a7baf3ff1">
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key>/*$0*/</key>
<value>/</value>
</Redirect>
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key>/2014-12-22</key>
<value>/</value>
</Redirect>
<Redirect xmlns="xmlns:xlink=http://www.w3.org/1999/xlink">
<key>/a</key>
<value>/</value>
</Redirect>
</Content>
我的代码
private void btnUpload_Click(object sender, EventArgs e)
{
if (lblFileUploaded.Text != "No_File_has_been_uploaded")
{
if (ddlHostName.SelectedIndex == -1)
{
MessageBox.Show("please_select_a_host_name");
}
else
{
var hostName = ddlHostName.SelectedItem.ToString();
//"Content" + "xmlns=\"uuid:5c2dffe6-cf72-4211-a8a0-dc6a7baf3ff1\""
XNamespace ns = "uuid:5c2dffe6-cf72-4211-a8a0-dc6a7baf3ff1";
var rootNode = new XElement(ns + "Content");
var processedUrls = new HashSet<string>();
//select the file thats been uploaded
var file = new FileInfo(openFileDialog1.FileName);
if (!file.Exists)
{
return;
}
if (file.Extension != ".xlsx")
{
lblFileUploaded.Text = "ERROR__File_format_invalid";
return;
}
using (var package = new ExcelPackage(file))
{
var workbook = package.Workbook;
var sheet = workbook.Worksheets[1];
int lastRow = sheet.Dimension.End.Row;
//loop through the xsls rows of data
for (int i = 2; i < lastRow; i++)
{
var oldUrl = sheet.Cells[i, 1].Text.Trim();
var newUrl = sheet.Cells[i, 2].Text.Trim();
if (string.IsNullOrWhiteSpace(oldUrl) || string.IsNullOrWhiteSpace(newUrl) || processedUrls.Contains(oldUrl))
continue;
processedUrls.Add(oldUrl);
XNamespace ns2 = "xmlns:xlink=" + "http://www.w3.org/1999/xlink";
var urlChildNode = new XElement( ns2 + "Redirect");
rootNode.Add(urlChildNode);
var urlOldNode = new XElement("key", oldUrl.Replace(hostName, ""));
urlChildNode.Add(urlOldNode);
if (newUrl.Equals(hostName + "/"))
{
var urlNewNode = new XElement("value", newUrl.Replace(hostName + "/", "/"));
urlChildNode.Add(urlNewNode);
}
else if (newUrl.Equals(hostName))
{
var urlNewNode = new XElement("value", newUrl.Replace(hostName, "/"));
urlChildNode.Add(urlNewNode);
}
else if (newUrl.Equals("www.aircharterservice.com"))
{
var urlNewNode = new XElement("value", newUrl.Replace("www.aircharterservice.com", "/"));
urlChildNode.Add(urlNewNode);
}
else
{
var urlNewNode = new XElement("value", newUrl.Replace(hostName, ""));
urlChildNode.Add(urlNewNode);
}
}
}
目前我正在努力查看如何删除不需要的命名空间
答案 0 :(得分:0)
感谢上面的帮助,以下代码帮助我解决了这个问题。
XNamespace ns2 = "xmlns:xlink=" + "http://www.w3.org/1999/xlink";
var urlChildNode = new XElement( ns2 + "Redirect");
rootNode.Add(urlChildNode);
var urlOldNode = new XElement(ns2 + "key", oldUrl.Replace(hostName, ""));
urlChildNode.Add(urlOldNode);
if (newUrl.Equals(hostName + "/"))
{
var urlNewNode = new XElement(ns2 + "value", newUrl.Replace(hostName + "/", "/"));
urlChildNode.Add(urlNewNode);
}
else if (newUrl.Equals(hostName))
{
var urlNewNode = new XElement(ns2 + "value", newUrl.Replace(hostName, "/"));
urlChildNode.Add(urlNewNode);
}
else if (newUrl.Equals("www.aircharterservice.com"))
{
var urlNewNode = new XElement(ns2 + "value", newUrl.Replace("www.aircharterservice.com", "/"));
urlChildNode.Add(urlNewNode);
}
else
{
var urlNewNode = new XElement(ns2 + "value", newUrl.Replace(hostName, ""));
urlChildNode.Add(urlNewNode);
}
答案 1 :(得分:0)
子元素从父级继承默认命名空间。所以,这个xml代码:
<parent xmlns="Namespace">
<child />
</parent>
相当于:
<parent xmlns="Namespace">
<child xmlns="Namespace" />
</parent>
但不是:
<parent xmlns="Namespace">
<child xmlns="" />
</parent>
因此,如果要从xmlns=""
元素中删除child
属性,则必须将child
元素放在与parent
元素相同的命名空间中,而不是空命名空间。