반응형
XML 문자열을 XmlElement로 변환해야합니다.
유효한 XML이 포함 된 문자열 XmlElement
을 C # 의 개체 로 변환하는 가장 간단한 방법을 찾고 있습니다.
이것을 어떻게 바꿀 수 XmlElement
있습니까?
<item><name>wrench</name></item>
이것을 사용하십시오 :
private static XmlElement GetElement(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
조심하세요 !! 이 요소를 먼저 다른 문서에 추가해야하는 경우를 사용하여 가져와야합니다 ImportNode
.
이미 자식 노드가있는 XmlDocument가 있고 문자열에서 자식 요소를 더 추가해야한다고 가정합니다.
XmlDocument xmlDoc = new XmlDocument();
// Add some child nodes manipulation in earlier
// ..
// Add more child nodes to existing XmlDocument from xml string
string strXml =
@"<item><name>wrench</name></item>
<item><name>screwdriver</name></item>";
XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
xmlDocFragment.InnerXml = strXml;
xmlDoc.SelectSingleNode("root").AppendChild(xmlDocFragment);
결과:
<root>
<item><name>this is earlier manipulation</name>
<item><name>wrench</name></item>
<item><name>screwdriver</name>
</root>
XmlDocument.LoadXml 사용 :
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
XmlElement root = doc.DocumentElement;
(또는 XElement에 대해 이야기하는 경우 XDocument.Parse를 사용하십시오. :)
XDocument doc = XDocument.Parse("<item><name>wrench</name></item>");
XElement root = doc.Root;
이를 위해 XmlDocument.LoadXml ()을 사용할 수 있습니다.
다음은 간단한 예입니다.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("YOUR XML STRING");
이 스 니펫으로 시도해 보았습니다.
// Sample string in the XML format
String s = "<Result> No Records found !<Result/>";
// Create the instance of XmlDocument
XmlDocument doc = new XmlDocument();
// Loads the XML from the string
doc.LoadXml(s);
// Returns the XMLElement of the loaded XML String
XmlElement xe = doc.DocumentElement;
// Print the xe
Console.out.println("Result :" + xe);
동일한 구현을위한 다른 더 좋고 효율적인 방법이 있으면 알려주십시오.
감사합니다 & 건배
참고URL : https://stackoverflow.com/questions/3703127/i-need-to-convert-an-xml-string-into-an-xmlelement
반응형
'programing tip' 카테고리의 다른 글
jQuery를 사용하여 단일 자식 요소를 선택하는 방법은 무엇입니까? (0) | 2020.12.11 |
---|---|
"Ctrl"/ "Shift"+ 마우스 왼쪽 버튼 클릭을위한 JavaScript 또는 jQuery 이벤트 핸들러 (0) | 2020.12.11 |
Django 템플릿 : 거짓이면? (0) | 2020.12.11 |
사이트에 포함 된 Javascript를 자동으로 축소 할 수있는 플러그인이 있습니까? (0) | 2020.12.11 |
hash_map이 STL의 일부입니까? (0) | 2020.12.11 |