以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  [求助]C#环境下,用DOM在XML文件写入某行内容求助  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=18382)


--  作者:easteast
--  发布时间:5/16/2005 2:58:00 PM

--  [求助]C#环境下,用DOM在XML文件写入某行内容求助
一个XML文件,比如a.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" ?>
<page>
</page>

想通过C#环境下DOM在<page></page>之间写入这样一行内容:
<xi:include href="pic1.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
应该如何使用DOM来读写上述内容?谢谢。盼指点。谢谢。


--  作者:ssm1226
--  发布时间:5/16/2005 3:55:00 PM

--  
string sXml=@"<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' ?>
<page>
</page>";
   XmlDocument xmldoc=new XmlDocument();
   xmldoc.LoadXml(sXml);
   //XmlNode oNode=xmldoc.DocumentElement;
   //添加一个新节点
   XmlNode oNewNode=xmldoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
   
   XmlAttribute attr=xmldoc.CreateAttribute("href");
   attr.Value="pic1.xml";
   oNewNode.Attributes.Append(attr);

   xmldoc.DocumentElement.AppendChild(oNewNode);
   Console.WriteLine(xmldoc.OuterXml);

   //匹配一节点
   XmlNamespaceManager nsmgr=new XmlNamespaceManager(xmldoc.NameTable);
   nsmgr.AddNamespace("xi","http://www.w3.org/2003/XInclude");
   
   XmlNode oNode=xmldoc.SelectSingleNode("//xi:include",nsmgr);
   if(oNode!=null)
    Console.WriteLine(oNode.OuterXml);
   else
    Console.WriteLine("none!");


--  作者:easteast
--  发布时间:5/16/2005 4:02:00 PM

--  
谢谢啊.非常感谢:)
--  作者:easteast
--  发布时间:5/17/2005 9:15:00 AM

--  
以下是引用ssm1226在2005-5-16 15:55:14的发言:
string sXml=@"<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' ?>
<page>
</page>";
    XmlDocument xmldoc=new XmlDocument();
    xmldoc.LoadXml(sXml);
    //XmlNode oNode=xmldoc.DocumentElement;
    //添加一个新节点
    XmlNode oNewNode=xmldoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
    
    XmlAttribute attr=xmldoc.CreateAttribute("href");
    attr.Value="pic1.xml";
    oNewNode.Attributes.Append(attr);

    xmldoc.DocumentElement.AppendChild(oNewNode);
谢谢。在完成内容加载后,我需要把这个动态生成的XML文档用
XmlTextReader r = new XmlTextReader(xmlname);来读的时候,
上面的参数xmlname可以用xmldoc的xmldoc.Name来获取。获取的名字是"#Document",可是系统找不到这个动态文件。应该如何把这个动态的xmldoc用XmlTextReader来读取?参数应该设置成什么路径下的名字?不把xmldoc保存后再来读取。因为这太耗时间空间了。盼指点。谢谢ssm和各位高手:)


    Console.WriteLine(xmldoc.OuterXml);

    //匹配一节点
    XmlNamespaceManager nsmgr=new XmlNamespaceManager(xmldoc.NameTable);
    nsmgr.AddNamespace("xi","http://www.w3.org/2003/XInclude");
    
    XmlNode oNode=xmldoc.SelectSingleNode("//xi:include",nsmgr);
    if(oNode!=null)
     Console.WriteLine(oNode.OuterXml);
    else
     Console.WriteLine("none!");



--  作者:ssm1226
--  发布时间:5/17/2005 9:31:00 AM

--  
string sXml=@"<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' ?>
<page>
</page>";
   XmlDocument xmldoc=new XmlDocument();
   xmldoc.LoadXml(sXml);
   //XmlNode oNode=xmldoc.DocumentElement;
   //添加一个新节点
   XmlNode oNewNode=xmldoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
   
   XmlAttribute attr=xmldoc.CreateAttribute("href");
   attr.Value="pic1.xml";
   oNewNode.Attributes.Append(attr);

   xmldoc.DocumentElement.AppendChild(oNewNode);
   
   XmlTextReader reader=new XmlTextReader(xmldoc.OuterXml,XmlNodeType.Document,null);
   
   while(reader.Read())
   {
    if(reader.NodeType==XmlNodeType.Element)
    {
     Console.WriteLine("Node:" + reader.Name);
     if(reader.HasAttributes)
     {
      while(reader.MoveToNextAttribute())
      {
       Console.WriteLine("attribute:" + reader.Name + ";;value:" + reader.Value);
      }
     }
    }
   }


--  作者:ssm1226
--  发布时间:5/17/2005 9:32:00 AM

--  
忘了reader.Close() 了:)
--  作者:easteast
--  发布时间:5/17/2005 9:53:00 AM

--  
以下是引用ssm1226在2005-5-17 9:31:22的发言:
string sXml=@"<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' ?>
<page>
</page>";
    XmlDocument xmldoc=new XmlDocument();
    xmldoc.LoadXml(sXml);
    //XmlNode oNode=xmldoc.DocumentElement;
    //添加一个新节点
    XmlNode oNewNode=xmldoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
    
    XmlAttribute attr=xmldoc.CreateAttribute("href");
    attr.Value="pic1.xml";
    oNewNode.Attributes.Append(attr);

    xmldoc.DocumentElement.AppendChild(oNewNode);
    
    XmlTextReader reader=new XmlTextReader(xmldoc.OuterXml,XmlNodeType.Document,null);
    谢谢啊。受益很多。感谢你。
其实我在xmldoc动态生成后,想通过调用一个函数,将其名称xmldoc.Name或者你说的这个方式xmldoc.OuterXml,XmlNodeType.Document,null作为参数,传给XmlTextReader来读取和执行XInclude。但我在编译运行后,提示“reader 此URI不存在”。


    while(reader.Read())
    {
     if(reader.NodeType==XmlNodeType.Element)
     {
      Console.WriteLine("Node:" + reader.Name);
      if(reader.HasAttributes)
      {
       while(reader.MoveToNextAttribute())
       {
        Console.WriteLine("attribute:" + reader.Name + ";;value:" + reader.Value);
       }
      }
     }
    }



--  作者:ssm1226
--  发布时间:5/17/2005 8:09:00 PM

--  
public void test()
  {
   string sXml=@"<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' ?>
<page>
</page>";
   XmlDocument xmldoc=new XmlDocument();
   xmldoc.LoadXml(sXml);
   //XmlNode oNode=xmldoc.DocumentElement;
   //添加一个新节点
   XmlNode oNewNode=xmldoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
   
   XmlAttribute attr=xmldoc.CreateAttribute("href");
   attr.Value="pic1.xml";
   oNewNode.Attributes.Append(attr);

   xmldoc.DocumentElement.AppendChild(oNewNode);
   reader(xmldoc.OuterXml);
   
   //Console.WriteLine(xmldoc.OuterXml);

   //匹配一节点
   XmlNamespaceManager nsmgr=new XmlNamespaceManager(xmldoc.NameTable);
   nsmgr.AddNamespace("xi","http://www.w3.org/2003/XInclude");
   
   XmlNode oNode=xmldoc.SelectSingleNode("//xi:include",nsmgr);
   if(oNode!=null)
    Console.WriteLine(oNode.OuterXml);
   else
    Console.WriteLine("none!");
  }
  private void reader(string sXml)
  {
   XmlTextReader reader=new XmlTextReader(sXml,XmlNodeType.Document,null);
   
   while(reader.Read())
   {
    if(reader.NodeType==XmlNodeType.Element)
    {
     Console.WriteLine("Node:" + reader.Name);
     if(reader.HasAttributes)
     {
      while(reader.MoveToNextAttribute())
      {
       Console.WriteLine("attribute:" + reader.Name + ";;value:" + reader.Value);
      }
     }
    }
   }
   reader.Close();
  }


--  作者:easteast
--  发布时间:5/18/2005 10:08:00 AM

--  
我的整体是这样的:
已经存在一个xml文件,a.xml:
里面已经有一个内容
<?xml version="1.0" encoding="GB2312" ?>
<?xml-stylesheet type="text/xsl" ?>
<page>
</page>

首先在里面插入XInclude的内容:
public XmlDocument xmloutput()
{
XmlDocument pdoc=new XmlDocument();
pdoc=mydis.Insert("page.xml","pic.xml");//这步完成调试,如果将文件保存,显示正常

//????就是这里需要调用xmlcreator的方法应该如何把pdoc的参数顺利传给xmlcreator,     
//然后返回一个经XInclude的XmlDocument,调用xmlcreator的参数传送不知道应该如何处理

return pagedoc;
}

//插入内容的方法:
public XmlDocument Insert(string filename,string includename)
{
XmlDocument mydoc=new XmlDocument();
mydoc.Load(Server.MapPath(filename));
XmlNode oNewNode=mydoc.CreateElement("xi","include","http://www.w3.org/2003/XInclude");
XmlAttribute attr=mydoc.CreateAttribute("href");
attr.Value=includename;
oNewNode.Attributes.Append(attr);
mydoc.DocumentElement.AppendChild(oNewNode);
return mydoc;
}

//执行XInclude的方法:(在这里面引用了MS的XInclude的库)
public XmlDocument xmlcreator(string xmlname)
{  
XmlTextReader r = new XmlTextReader(xmlname);
XIncludingReader xir = new XIncludingReader(r);
XmlDocument doc = new XmlDocument();
doc.Load(xir);
r.Close();
return doc;
}


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