如何添加到xml的链接 [英] How to add links to xml

查看:85
本文介绍了如何添加到xml的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写信寻求帮助,在下面添加'href'/'uri'节点链接到我的xml文档代码。我想要的输出,我的目标如下所示:

  protected   void  Page_Load( object  sender,EventArgs e)
{
string xmlString = <?xml version ='1.0'coding ='UTF-8'?> ;
xmlString + = < Pub>;
xmlString + = < pub_code ='p1'>;
xmlString + = < name> News< / name>;
xmlString + = < Category> NL< / Category>;
xmlString + = < / pub_code>;
xmlString + = < / Pub>;

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
doc.Save(Server.MapPath( products-string.xml));

Response.Redirect( products-string.xml); // 在浏览器中加载文件
}





所需输出:

<前lang =HTML> < pub code = ABC uri = https://api.####.com/##### ## >
< name > ... < / name >
< category > ... < / category > ;
< link rel = http:// api -info。#########。com / v2 /###/######.####\"

href = https://api.####.com/###/### title = 问题 / >
< span class =code-keyword>< / publication >





我尝试在xml代码中添加链接,但我一直在遇到编译错误。是他的示例或我可以遵循的一些指南,以使我的代码显示所需的输出。



非常感谢您的帮助

解决方案

您可以使用 XmlDocument XDocument (LINQ)。

这是一个选择问题,但重点是这些类是为创建格式良好的XML结构。



这是使用 XDocument XElement的示例

它会为你提供你想要的输出。

 XElement xeRoot =  new  XElement(  publication); 
xeRoot.Add( new XAttribute( 代码 ABC));
xeRoot.Add( new XAttribute( uri @ https://api.####.com/###/ ###));

XElement xeName = new XElement( );
// 将数据添加到名称节点。
// ...
xeRoot.Add(xeName);

XElement xeCategory = new XElement( 类别);
// 将数据添加到类别节点。
// ...
xeRoot.Add(xeCategory);

XElement xeLink = new XElement( 链接);
xeLink.Add( new XAttribute( rel @ http://api-info.#########的.com / V2 /###/######.####\"));
xeLink.Add( new XAttribute( href @ https://api.####.com/###/ ###));
xeLink.Add( new XAttribute( title 问题));
xeRoot.Add(xeLink);

XDocument xDoc = new XDocument(xeRoot);
xDoc.Save( products-string.xml);






请试试这个

  protected   void  Page_Load( object  sender,EventArgs e)
{
StringBuilder xmlString = new StringBuilder();
xmlString.Append( <?xml version = \1.0 \encoding = \ UTF-8\ >?);
xmlString.Append( < pub code = \ABC \uri = \ HTTPS://api.####.com/###/###\ > 中);
xmlString.Append( < category> ...< / category> );
xmlString.Append( < link rel = \http://api-info.# ########。com / v2 /############### \href = \https://api.####.com/# ## / ### \title = \Issue \/>);
xmlString.Append( < / pub>);

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString.ToString());
doc.Save(Server.MapPath( products-string.xml));
Response.Redirect( products-string.xml);
}


I am writing to seek help, in adding 'href'/'uri' node link to my xml document code below. My desired output, I aiming for as shown below:

protected void Page_Load(object sender, EventArgs e)
   {
       string xmlString = "<?xml version='1.0' encoding='UTF-8'?>";
     xmlString += "<Pub>";
     xmlString += "<pub_code ='p1'>";
       xmlString += "<name>News</name>";
         xmlString += "<Category>NL</Category>";
       xmlString += "</pub_code>";
     xmlString += "</Pub>";

     XmlDocument doc = new XmlDocument();
     doc.LoadXml(xmlString);
     doc.Save(Server.MapPath("products-string.xml"));

     Response.Redirect("products-string.xml"); // load file in browser
   }



Desired output:

<pub code="ABC" uri="https://api.####.com/###/###">
<name>...</name>
<category>...</category>
<link rel="http://api-info.#########.com/v2/###/######.####"

href="https://api.####.com/###/###" title="Issue" />
</publication>



I have tried adding the link within the xml code, but I keep experiencing compiling errors. Is their example or some guide I could follow, to get my code to display the desired output.

Many thanks for your help

解决方案

You can use XmlDocument or XDocument (LINQ).
It is a matter of choice, but the point is that these classes are made for creating well formatted XML structures.

This is an example using XDocument and XElement.
It will give you the output you want.

XElement xeRoot = new XElement("publication");
xeRoot.Add(new XAttribute("code", "ABC"));
xeRoot.Add(new XAttribute("uri", @"https://api.####.com/###/###"));

XElement xeName = new XElement("name");
// Add data to the name node.
// ...
xeRoot.Add(xeName);

XElement xeCategory = new XElement("category");
// Add data to the category node.
// ...
xeRoot.Add(xeCategory);

XElement xeLink = new XElement("link");
xeLink.Add(new XAttribute("rel", @"http://api-info.#########.com/v2/###/######.####"));
xeLink.Add(new XAttribute("href", @"https://api.####.com/###/###"));
xeLink.Add(new XAttribute("title", "Issue"));
xeRoot.Add(xeLink);

XDocument xDoc = new XDocument(xeRoot);
xDoc.Save("products-string.xml");


Hi,

Please try this

protected void Page_Load(object sender, EventArgs e)
{
  StringBuilder xmlString = new StringBuilder();
  xmlString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  xmlString.Append("<pub code=\"ABC\" uri=\"https://api.####.com/###/###\">");
  xmlString.Append("<category>...</category>");
  xmlString.Append("<link rel=\"http://api-info.#########.com/v2/###/######.####\" href=\"https://api.####.com/###/###\" title=\"Issue\" />");
  xmlString.Append("</pub>");

  XmlDocument doc = new XmlDocument();
  doc.LoadXml(xmlString.ToString());
  doc.Save(Server.MapPath("products-string.xml"));
  Response.Redirect("products-string.xml");
}


这篇关于如何添加到xml的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆