Asp.Net写入读取Xml(处理文件权限)
1,网上关于读取写入Xml的博客比较多,参考了发现提到Xml文件权限的博客比较少。因为在开发中我发现,如果文件存于一些没有权限的路径,代码是访问不到该文件,页面会报错提示403,Forbidden。意思是禁止,也就是没有权限。需要用代码给文件EveryOne赋予完全控制权限。希望我的博客能帮助一些在权限方面遇到问题的朋友。
2,判断文件文件夹和文件是否存在(写入时会自动创建Xml,但是如果没有权限,会创建失败,所以我觉得先用FileStream把文件创建出来比较保险);
public string CreateFolder()
{
string fileName = "myXml";
string folderPath = "C:\\Configurations";
string filePath = @"C:\\Configurations\" + fileName + ".xml";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
//给文件夹Everyone赋完全控制权限
DirectorySecurity folderSec = new DirectorySecurity();
folderSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
System.IO.Directory.SetAccessControl(folderPath, folderSec);
CreateFile(filePath); }
else
{
CreateFile(filePath);
}
return filePath;
}
public void CreateFile(string filePath)
{
if (!File.Exists(filePath))
{
using (FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
//给Xml文件EveryOne赋完全控制权限
DirectorySecurity fSec = new DirectorySecurity();
fSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
System.IO.Directory.SetAccessControl(filePath, fSec);
} }
}
3,文件夹和文件都创建出来以后就是写入了。
(1)Xml有几个重要的对象。、|XmlDocument,Xml文档对象|XmlDeclaration,Xml文档定义对象|XmlElement,Xml节点对象|XmlAttrbute,Xml节点属性对象|
了解了这几个对象,开发起来就比较顺了。
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "张三", Age = , Email = "hl@yahoo.com" });
list.Add(new Person() { Name = "李四", Age = , Email = "xzl@yahoo.com" });
list.Add(new Person() { Name = "王五", Age = , Email = "hhw@yahoo.com" });
list.Add(new Person() { Name = "赵六", Age = , Email = "ys@yahoo.com" }); //1.创建一个Dom对象
XmlDocument xDoc = new XmlDocument();
//2.编写文档定义
XmlDeclaration xmlDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xDoc.AppendChild(xmlDec); //3.编写一个根节点
XmlElement xmlRoot = xDoc.CreateElement("List");
xDoc.AppendChild(xmlRoot); //4.循环创建Person节点
for (int i = ; i < list.Count; i++)
{
//4.1创建一个Person元素
XmlElement xmlPerson = xDoc.CreateElement("Person");
XmlAttribute xmlAttrId = xDoc.CreateAttribute("id");
xmlAttrId.Value = (i + ).ToString();
//将属性增加到Person节点中
xmlPerson.Attributes.Append(xmlAttrId); //4.2在这里向Person节点下增加子节点
//创建Name
XmlElement xmlName = xDoc.CreateElement("Name");
xmlName.InnerText = list[i].Name;
xmlPerson.AppendChild(xmlName); //创建Age
XmlElement xmlAge = xDoc.CreateElement("Age");
xmlAge.InnerText = list[i].Age.ToString();
xmlPerson.AppendChild(xmlAge); //创建一个Email节点 XmlElement xmlEmail = xDoc.CreateElement("Email");
xmlEmail.InnerText = list[i].Email;
xmlPerson.AppendChild(xmlEmail); //最后把Person加到根节点下
xmlRoot.AppendChild(xmlPerson); } //5.将xmlDocument对象写入到文件中
xDoc.Save(@"C:\Configurations\myXml.xml");
4,Xml读取
public DataTable GetDataFromXml()
{
string fileName = "myXml";
string filePath = @"C:\\Configurations\" + fileName + ".xml";
DataTable dt = this.BuildDataTable();
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlElement rootElement = document.DocumentElement; dt = LoadToTreeByXmlDocument(rootElement, dt); return dt;
}
catch
{
return dt;
}
}
private DataTable LoadToTreeByXmlDocument(XmlElement rootElement, DataTable dt)
{
try
{
foreach (XmlNode node in rootElement.ChildNodes)
{
if (node.NodeType == XmlNodeType.Element)
{
DataRow dr = dt.NewRow();
foreach (DataColumn dc in dt.Columns)
{
dr[dc.ColumnName] = node.Attributes[dc.ColumnName] == null ? "" : node.Attributes[dc.ColumnName].Value;
}
dt.Rows.Add(dr);
//遍历二级节点
foreach (XmlNode subNode in node.ChildNodes)
{
if (subNode.NodeType == XmlNodeType.Element)
{
DataRow subDr = dt.NewRow();
foreach (DataColumn dc in dt.Columns)
{
subDr[dc.ColumnName] = subNode.Attributes[dc.ColumnName] == null ? "" : subNode.Attributes[dc.ColumnName].Value;
}
dt.Rows.Add(subDr);
}
}
}
}
return dt;
}
catch
{
return dt;
}
}
Asp.Net写入读取Xml(处理文件权限)的更多相关文章
- asp.net写入读取xml的方法
添加命名空间 using System.Xml; 我自己的代码(添加其中的节点) XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.M ...
- Android中写入读取XML
获取XML文件的基本思路是,通过getResources().getXml()获的XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开头还是结尾,是某个标签的开始还是 ...
- RSS阅读器(一)——dom4j读取xml(opml)文件
接触java不久,偶有收获,最近想做一个web版RSS阅读器来锻炼一下.手头有几个从不同版本的foxmail中导出的opml文件,大家应该都知道,opml文件就是xml格式的.那么就先从这里入手,练习 ...
- ASP.NET MVC读取XML并使用ViewData显示
看到网上一个网友问及,无法获取XML某一个节点内容.下面Insus.NET在ASP.NET MVC环境下实现它. 先把XML文件放入App_Data目录,当然你可以放在自建目录中.打开看看它有几层,几 ...
- asp.net core读取appsetting.json文件
1.在Startup.cs文件中注入,ConfigureServices方法 services.Configure<MyConfig>(Configuration.GetSection(& ...
- 读取xml格式文件
$v = [xml]get-content d:\vmconfig.xml $v.Domain.Computer.Name =========================== $v.GetElem ...
- ASP.NET写入和读取xml文件
xml是一种可扩展标记语言,在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等.它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 ...
- 读取xml文件,写入excel
在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件. 输入xml格式: <?xml version="1.0&qu ...
- C#读取xml文件写入到TreeView中
开发过程中我们会遇到一些读取xml文件的时候,下面是我学习的整理. 用XmlDocument读取加载 XmlDocument doc = new XmlDocument(); doc.Load(&qu ...
随机推荐
- 自动补全Typeahead
采用 Typeahead (Bootstrap-3-Typeahead-master) <script type="text/javascript" src="/j ...
- Angular面试题四
二十.angular 的缺点有哪些? 1.强约束 导致学习成本较高,对前端不友好. 但遵守 AngularJS 的约定时,生产力会很高,对 Java 程序员友好. 2.不利于 SEO 因为所有内容都是 ...
- <Android 基础(二十九)> Fragment (2) ~ DialogFragment
简介 上一篇简单的介绍了下Fragment的使用方法,这一篇主要看下DialogFragment. 在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示 ...
- idea 断点调试
一.步骤控制 二.查看变量 IDEA debug查看变量值有3种方法: 1.鼠标悬浮 2.alt+f8快捷键(选中变量表达式,比如匿名变量或方法参数,再按atl+f8,接着回车) 3.debug窗口查 ...
- java 内存分析之构造方法执行过程
package Demo; public class BirthDate { private int day; private int month; private int year; public ...
- 带你从零学ReactNative开发跨平台App开发(三)
ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...
- PyQt4(使用ui)
1.使用qt designer设计界面,保存为test1.ui: 2.使用pyuic4 test1.ui -o ui.py生成ui代码. 3.程序载入. import sys import ui fr ...
- 【Java】数组使用
package aaa; public class aaa { public static void main(String args[]) { int a[]={1,2,3,4}; for(int ...
- Sql Server tempdb原理-启动过程解析实践
我们知道在SqlServer实例启动过程中数据库会进行还原(Redo,Undo)然后打开提供服务,但我们知道tempdb是不提供重做机制的(Redo)那tempdb是如何还原的呢?如果tempdb损坏 ...
- 使用CADisplayLink写秒表
使用CADisplayLink写秒表 效果: 源码: StopWatch.h 与 StopWatch.m // // StopWatch.h // ShowTime // // Created by ...