iNode.selectSingleNode('@abc).text := 'abc123';
这句在iNode 中有 abc时才有效.如何当没有abc的时候添加 attribute?还有一个问题
取 iNode.selectSingleNode('@abc').text 时, 
是不是必须判断 
iNode.selectSingleNode('@abc') = nil有没有办法 iNode.selectSingleNode('@abc') = nil 时, iNode.selectSingleNode('@abc').text 自动= '', 而不出错.谢谢.

解决方案 »

  1.   

    是用的XMLDocument吗,好像不是如果是XMLDocument的话
    如Delphi带的Example 
    //Turbo Delphi Help[ms-help://borland.bds4/bds4win32devguide/html/xmldocworkingwithxmlnodes.htm]BorlandStock := XMLDocument1.DocumentElement.ChildNodes[0];
    BorlandStock.ChildNodes['shares'].Attributes['type'] := 'common';
    这儿如果不存在type属性,则自动添加type=commonBorlandStock.ChildNodes['shares'].Attributes['type'] := nil;
    这样删除type属性
      

  2.   

    不是 XMLDocument.IXMLDOMDocument
      

  3.   

    这不符合XPATH语法。
    if iNode.selectSingleNode('//@abc) <> Nil then
       iNode.selectSingleNode('//@abc).text := 'abc123';
      

  4.   

    to 陈如何当 iNode 不存在 attribute abc时, 可以自动创建attribute abc?就像 小虫 说的 XMLDocument那样方便.
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Root,Node:IXMLDOMNode;
      XMLFile:IXMLDOMDocument;
    begin
      XMLFile:=CreateDOMDocument();
      XMLFile.load('d:\test.xml');
      root:=XMLFile.documentElement;
      Node:=Root.selectSingleNode('/abc');
      if Node <> Nil then
        Node.text := 'abc123'
      else
        begin
        Node:=XMLFile.createNode(1,'abc','');
        Node.text:='abc123';
        Root.appendChild(Node);
        XMLFile.save('d:\test.xml');
        end;
    end;
      

  6.   

    自己编一个函数吧。AssignNodeAttr( node, attrName, value );
    var
      attr: IXMLDOMNode;
    begin
      attr := node.selectSingleNode(attrName);
      if (value=Nil) then
      begin
        处理置空情况
        exit;
      end;  if (attr=nil) then
      begin
        attr := node.ownerDocument.createNode(attrName);
        Node.appendChild(attr);
      end;
      attr.text := value;
    end;