下面为源代码,运行添加时,倒会成功的,但是第一行为一空白行,即所字段的值为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);

解决方案 »

  1.   

    我试了没有你说的问题呀,你的xml是什么样的?
      

  2.   

    这是guestbook.xsd文件。<?xml version="1.0" encoding="utf-8" ?>
    <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>
      

  3.   

    不知道你说的空白是什么意思。
    我测试前的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是怎么读取的。
      

  4.   

    我说的空白行是这样的:mail      name     title    contentnull      null     null     null
    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行出错。