[英]How to Handle XML string using VB.net where attribute have html value
[英]How to add attribute to Xmlns in Xml file using VB.Net
我需要屬性添加到Xmlns
在XML
使用VP.Net文件,我用這個代碼:
Dim ns As XNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9"
Dim xi As XNamespace = "http://www.w3.org/1999/xhtml"
Dim doc As New XmlDocument
Dim decNode As XmlNode = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
doc.AppendChild(decNode)
Dim parentNode As XmlNode
parentNode = doc.CreateElement("xmlns")
doc.AppendChild(parentNode)
Dim childNode1 As XmlNode = doc.CreateElement("url")
parentNode.AppendChild(childNode1)
Dim sitemap_loc As String = "https://example.com/" & "example.aspx?tdc_id=" & dtvTender.Table.Rows(tender).Item(CMS_Tender_Category.Fields.TDC_ID)
Dim sitemap_lastmod As String = Date.Now.ToString("yyyy-MM-dd")
Dim childElement1 As XmlElement = doc.CreateElement("loc")
childElement1.InnerText = sitemap_loc
childNode1.AppendChild(childElement1)
Dim childElement2 As XmlElement = doc.CreateElement("lastmod")
childElement2.InnerText = sitemap_lastmod
childNode1.AppendChild(childElement2)
Dim strfilepath As String = Server.MapPath("~/SiteMap/") & Date.Now.ToString("dd-MM-yyyy") & ".xml"
doc.Save(strfilepath)
我需要在xmlns
標簽內添加此屬性
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml
如果要創建站點地圖 ,可能更容易使用XDocument
:
'Sample Data: You don't need this, this is just sample data used below.
' But if you do (e.g. for testing), you'll need to reference System.ServiceModel
Dim syndicationItems = New List(Of SyndicationItem) From {
New SyndicationItem("page 1", "Description 1", New Uri("htto://domain.com/page1.html"), "id1", DateTimeOffset.Now),
New SyndicationItem("page 2", "Description 2", New Uri("htto://domain.com/page2.html"), "id2", DateTimeOffset.Now),
New SyndicationItem("page 3", "Description 3", New Uri("htto://domain.com/page3.html"), "id3", DateTimeOffset.Now)
}
Dim xdoc = New XDocument(New XDeclaration("1.0", "UTF-8", Nothing))
Dim ns As XNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9"
Dim urlSet as New XElement(ns + "urlset")
syndicationItems.ForEach(Sub(s As [SyndicationItem])
Dim url as New XElement(ns + "url")
url.Add(New XElement(ns + "loc", s.Links.First().Uri.ToString()))
url.Add(New XElement(ns + "lastmod", s.LastUpdatedTime.ToString("yyyy-MM-dd")))
urlset.Add(url)
End Sub)
xdoc.Add(urlSet)
'....
'xdoc.Save(strfilepath)
輸出:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>htto://domain.com/page1.html</loc>
<lastmod>2017-03-05</lastmod>
</url>
<url>
<loc>htto://domain.com/page2.html</loc>
<lastmod>2017-03-05</lastmod>
</url>
<url>
<loc>htto://domain.com/page3.html</loc>
<lastmod>2017-03-05</lastmod>
</url>
</urlset>
心連心...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.