xml in asp read and write
I want to read xml file in asp.net use c# and also to write I tried this code
using (XmlReader reader = XmlReader.Create(Server.MapPath("TestXml.xml")))
{
reader.Read();
while (reader.Read())
{
Response.开发者_如何转开发Write( reader.Value.ToString());
Response.Write("<br />");
}
}
and the output is:
and the xml is
<?xml version="1.0" encoding="utf-8" ?>
<note>
<to>Chayem</to>
<from>Shaul</from>
<heading>Way?</heading>
<body>because</body>
</note>
What do I do? Thank you.
XmlReader.Read()
always processes the next node in the document. Note: node, not element. If you step through that code and examine each node, you'll see that while there are only six elements in the XML document, the XmlReader
is processing eleven nodes.
This is because your XML document (like most) has text nodes containing whitespace characters between the elements. Your code is writing a <br/
> to the response every time the XmlReader
encounters a node. What you want is to write one out every time the XmlReader
encounters an element with text content.
A way to do this is to simply only write out nodes that contain something other than whitespace, e.g.:
if (!string.IsNullOrEmpty(reader.Value.ToString().Trim())
{
Response.Write(reader.Value.ToString())
Response.Write("<br/>");
}
But why are you even using an XmlReader
? There are much easier ways to generate this output. Load the XML into an XDocument
, for instance, and you can produce the output like this:
Response.Write(string.Join("<br/>", doc.Descendants()
.Where(x => (!string.IsNullOrEmpty(x.Value.ToString().Trim())
.Select(x => x.Value.ToString())
.ToArray());
I think the problem is that XmlReader reads also the \n
parts of the XML.
Try to use something like this:
using (XmlReader reader = XmlReader.Create(Server.MapPath("feedback.xml")))
{
while (reader.Read())
{
if (reader.Value == "\n")
{
Response.Write("<br />");
}
else
{
Response.Write(reader.Value);
}
}
}
EDIT: You've said there were too many spaces between each line in the output. Considering XmlReader reads also the \n
characters, using your method added extra empty lines in the result. My solution is to insert a <br />
tag only when the reader encounters a new line.
Writing example:
public void WriteXml(List<string> filePaths, List<string> fileName)
{
//creating xml file
XmlElement mainNode, descNode;
XmlText pathText, nameText;
XmlDocument xmlDoc = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("ImageStore");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
for (int i = 0; i < filePaths.Count; i++)
{
//Console.WriteLine("first xml image "+i);
// Create a new <Category> element and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("Image");
// Set attribute name and value!
parentNode.SetAttribute("ID", "01");
xmlDoc.DocumentElement.PrependChild(parentNode);
// Create the required nodes
mainNode = xmlDoc.CreateElement("URL");
descNode = xmlDoc.CreateElement("Name");
//XmlElement activeNode = xmlDoc.CreateElement("Active");
// retrieve the text
pathText = xmlDoc.CreateTextNode(filePaths[i]);
nameText = xmlDoc.CreateTextNode(fileName[i]);
XmlText activeText = xmlDoc.CreateTextNode("true");
// append the nodes to the parentNode without the value
parentNode.AppendChild(mainNode);
parentNode.AppendChild(descNode);
parentNode.AppendChild(activeNode);
// save the value of the fields into the nodes
mainNode.AppendChild(pathText);
descNode.AppendChild(nameText);
activeNode.AppendChild(activeText);
}//end of for loop
// Save to the XML file. Checks if previous file exist
if (File.Exists(Application.StartupPath + "/imageStore.xml"))
{
File.Delete(Application.StartupPath + "/imageStore.xml");
xmlDoc.Save(Application.StartupPath + "/imageStore.xml");
}
else
{
xmlDoc.Save(Application.StartupPath + "/imageStore.xml");
}
//return true;
}//end of writeXml
Reading example:
XmlDocument doc = new XmlDocument();
doc.Load("./config.xml");
XmlNodeList configSet = doc.GetElementsByTagName("config");
foreach (XmlNode node in configSet)
{
XmlElement conElement = (XmlElement)node;
configData.Add("raw",conElement.GetElementsByTagName("raw")[0].InnerText);
//Console.WriteLine(raw + " space " + store + " space " + achieve + " space " + filter);
}
the xml file look like this:
<?xml version="1.0" encoding="utf-8"?>
<ConfigFile>
<config>
<raw>./raw/</raw>
<store>./images/</store>
<achieve>./achieve/</achieve>
<filter>*.jpg;*.png;*.gif</filter>
</config>
</ConfigFile>
Hope this helps you
精彩评论