以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML基础 』  (http://bbs.xml.org.cn/list.asp?boardid=1)
----  在C#中操作XML文件出现的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=57744)


--  作者:superzf
--  发布时间:1/5/2008 9:21:00 PM

--  在C#中操作XML文件出现的问题
刚开始接触XML,遇到一个问题,采用C#往db.xml中写内容,下面将db.xml内容贴在下面:<?xml version="1.0" encoding="utf-8" standalone="no"?>
<databases>
  <database name="local" connectionString="Provider=sqloledb;Data Source=192.168.0.26;User Id=sa;Password=;" type="sqlserver" />
  <database name="zl" connectionString="Provider=sqloledb;Data Source=192.168.0.66;User Id=sa;Password=5209;" type="sqlserver" />
</databases>
当 databases 中不加 xmlns="http://tempuri.org/db.xsd",时可以写入成功,加上后,添加内容时显示
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 64:         objElement.SetAttribute("type", type);
行 65:
行 66:         root.AppendChild(objElement);
行 67:         xmlDoc.Save(Server.MapPath("../Config/db.xml"));
行 68:     }

db.xsd内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="db"
    targetNamespace="http://tempuri.org/db.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/db.xsd"
    xmlns:mstns="http://tempuri.org/db.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="databases">
    <xs:complexType>
      <xs:sequence>
        
          <xs:element name="database" maxOccurs="unbounded">
            <xs:complexType>
              <xs:attribute name="name" type="xs:string"/>
              <xs:attribute name="connectionString" type="xs:string"/>
              <xs:attribute name="type" type="xs:string"/>
            </xs:complexType>
          </xs:element>
       
      </xs:sequence>

    </xs:complexType>
  </xs:element>
</xs:schema>

请各位大侠帮忙指导一下


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms