Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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:
HTML
<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
Posted
Comments
Sergey Alexandrovich Kryukov 2-Sep-14 13:08pm    
First, if this is your desired output, why not populating your XML with desired data, instead of something else?
Secondly, the approach is bad. If you are already using XmlDocument, why having this XML text input, why not manipulating this object directly? The overall goal of your manipulations is not clear.
—SA

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.
C#
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");
 
Share this answer
 
Comments
miss786 3-Sep-14 5:09am    
Hi, Thank you very much for your time and help. This has been a great help.
George Jonsson 3-Sep-14 5:21am    
You are welcome. :)
Hi,

Please try this
C#
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");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900