下面为源代码,运行添加时,倒会成功的,但是第一行为一空白行,即所字段的值为null,第二行才是添加的数据,这样再用datagrid读取这个xml文件时,就会出错,说有空白行什么的。不知下面的代码有什么问题,请大家指教了,一上午都是这个问题呀。XmlDocument doc = new XmlDocument();string FilePath = "\\inetpub\\wwwroot\\GuestBook\\other.xml";//加载XML文件
doc.Load(FilePath);//查找根元素
XmlElement elem = doc.DocumentElement;//新建XML元素guest
XmlElement newelem = doc.CreateElement("guest","http://tempuri.org/guestbook.xsd");//新建XML各子元素
XmlElement elem1 = doc.CreateElement("mail","http://tempuri.org/guestbook.xsd");
XmlElement elem2 = doc.CreateElement("name","http://tempuri.org/guestbook.xsd");
XmlElement elem3 = doc.CreateElement("title","http://tempuri.org/guestbook.xsd");
XmlElement elem4 = doc.CreateElement("content","http://tempuri.org/guestbook.xsd");//为各子元素赋值
elem1.InnerText = mail.Text;
elem2.InnerText = name.Text;
elem3.InnerText = title.Text;
elem4.InnerText = content.Text;//添加到guest中
newelem.AppendChild(elem1);
newelem.AppendChild(elem2);
newelem.AppendChild(elem3);
newelem.AppendChild(elem4);
elem.AppendChild(newelem);//保存XML文件
doc.Save(FilePath);
doc.Load(FilePath);//查找根元素
XmlElement elem = doc.DocumentElement;//新建XML元素guest
XmlElement newelem = doc.CreateElement("guest","http://tempuri.org/guestbook.xsd");//新建XML各子元素
XmlElement elem1 = doc.CreateElement("mail","http://tempuri.org/guestbook.xsd");
XmlElement elem2 = doc.CreateElement("name","http://tempuri.org/guestbook.xsd");
XmlElement elem3 = doc.CreateElement("title","http://tempuri.org/guestbook.xsd");
XmlElement elem4 = doc.CreateElement("content","http://tempuri.org/guestbook.xsd");//为各子元素赋值
elem1.InnerText = mail.Text;
elem2.InnerText = name.Text;
elem3.InnerText = title.Text;
elem4.InnerText = content.Text;//添加到guest中
newelem.AppendChild(elem1);
newelem.AppendChild(elem2);
newelem.AppendChild(elem3);
newelem.AppendChild(elem4);
elem.AppendChild(newelem);//保存XML文件
doc.Save(FilePath);
解决方案 »
- 查询的多条记录存入数据表
- 如何获取excel的所有工作薄的数据
- WIN8 怎么安装SQL2008
- <C#设计模式 + 框架设计>群号:8016846
- 大连高薪园区或软件园工作的软件编程人员请加QQ群5244759,有机会一起做个项目.
- 我的电脑是不是疯了啊,这个是什么错???
- 20分求MSSQL教程....
- Xmlhttp如何截获发送到服务器后未转向的返回信息?
- *︻$▅▆▇◤ 22分捉拿高手,比较考智力,只求一句提示即可!
- XML节点后跟分号,读取错误怎么办
- ASP.NET(C#)中如何将数据库中存放的word文档显示到aspx页面中,急,在线等!
- .NET类库里有没有将<转换成<和将<转换成<等类似字符转换功能的类和方法
<xs:schema id="guestbook" targetNamespace="http://tempuri.org/guestbook.xsd" elementFormDefault="qualified"
xmlns="http://tempuri.org/guestbook.xsd" xmlns:mstns="http://tempuri.org/guestbook.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="guest">
<xs:complexType>
<xs:sequence>
<xs:element name="mail" type="xs:string" />
<xs:element name="name" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element name="content" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>添加完一条记录后,other.xml文件变成下列的代码。<?xml version="1.0" encoding="utf-8" ?>
<guest xmlns="http://tempuri.org/guestbook.xsd">
<guest>
<mail>a</mail>
<name>s</name>
<title>d</title>
<content>f</content>
</guest>
</guest>
我测试前的XML文档为
----------------------------------
<?xml version="1.0"?>
<SendMail>
</SendMail>
-----------------------------------
添加后的XML为
-----------------------------------
<?xml version="1.0"?>
<SendMail>
<guest xmlns="http://tempuri.org/guestbook.xsd">
<mail>mail</mail>
<name>name</name>
<title>title</title>
<content>content</content>
</guest>
</SendMail>
--------------------------------
你那边ds是怎么读取的。
a s d f即第一次添加时,总是会有一行null这样的空白行,这样读取时就提示有错误。我读取的代码如下:
DataSet ds = new DataSet();
FileStream fs = new FileStream(Server.MapPath("guestbook.xsd"),FileMode.Open,FileAccess.Read);
StreamReader schema = new StreamReader(fs);
ds.ReadXmlSchema(schema);
fs.Close();fs = new FileStream(Server.MapPath("other.xml"),FileMode.Open,FileAccess.Read);
StreamReader reader = new StreamReader(fs);
ds.ReadXml(reader);
fs.Close();DataView Source = new DataView(ds.Tables[0]);
this.DataGrid1.DataSource = Source;
this.DataGrid1.DataBind();运行时的错误如下:
未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.ConstraintException: 未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。源错误:
行 36: fs = new FileStream(Server.MapPath("other.xml"),FileMode.Open,FileAccess.Read);
行 37: StreamReader reader = new StreamReader(fs);
行 38: ds.ReadXml(reader);
行 39: fs.Close();在上面的第38行出错。