<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/XMLNode/element(withName:stringValue:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSXMLNode(cm)elementWithName:stringValue:"
  },
  "title" : "element(withName:stringValue:)"
}
-->

# element(withName:stringValue:)

Returns an [`XMLElement`](/documentation/Foundation/XMLElement) object with a single text-node child containing the specified text.

```
class func element(withName name: String, stringValue string: String) -> Any
```

## Parameters

`name`

A string that is the name (tag identifier) of the element.

`string`

A string that is the content of the receiver’s text node.

## Return Value

An `NSXMLElement` object with a single text-node child—an `NSXMLNode` object of kind [`XMLNode.Kind.text`](/documentation/Foundation/XMLNode/Kind-swift.enum/text)—containing the text specified in `string`. Returns `nil` if the object couldn’t be created.

## Discussion

The equivalent XML markup is `<``name``>``string``</``name``>`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)