Adding an XML Node

How to add an node to an XML Document (via a C# helper that can be called in an expression shape from an orchestration).


First example (uses AppendChild):


{!{code}!}czozMjE3OlwiCiAgICAgICAgcHVibGljIHN0YXRpYyBYbWxEb2N1bWVudCBBZGRFeGNlcHRpb25Ob2RlVG9UTVhNTChYbWxEb2N1bWV7WyYqJl19bnQgVE1YTUxEb2NJbiwgRXhjZXB0aW9uIGV4KQogICAgICAgIHsKICAgICAgICAgICAgLy8gdGhyZWUgaW1wb3J0IGZpZWxkcyBpbntbJiomXX0gU3lzdGVtLkV4Y2VwdGlvbiBhcmUgZXguTWVzc2FnZSwgZXguU3RhY2tUcmFjZSwgZXguU291cmNlCiAgICAgICAgICAgIC8vIHRoe1smKiZdfWVuIHRoZXJlIGlzIGFuIEV4LklubmVyRXhjZXB0aW9uCgogICAgICAgICAgICBYbWxEb2N1bWVudCB4bWxEb2NOZXdUTVhNTCA9IG57WyYqJl19ZXcgWG1sRG9jdW1lbnQoKTsKICAgICAgICAgICAgeG1sRG9jTmV3VE1YTUwuTG9hZFhtbChUTVhNTERvY0luLk91dGVyWG1sKTsgIHtbJiomXX0vLyBtYWtlIHN1cmUgd2UgZG9uXCd0IHVwZGF0ZSB0aGUgb3JpZ2luYWwgVE1YTUxEb2NJbgoKICAgICAgICAgICAgc3RyaW5nIGVycntbJiomXX1NZXNzYWdlID0gXCJcIjsKICAgICAgICAgICAgc3RyaW5nIGVyclNvdXJjZSA9IFwiXCI7CiAgICAgICAgICAgIHN0cmluZyBlcnJTdGFja3tbJiomXX1UcmFjZSA9IFwiXCI7CiAgICAgICAgICAgIHN0cmluZyB4bWw7CgogICAgICAgICAgICBpZiAoZXguSW5uZXJFeGNlcHRpb24gPT0gbnV7WyYqJl19bGwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIGlmIG5vdCBuZXN0ZWQgZXhjZXB0aW9ucywgZG8gbm90IG51bWJlcntbJiomXX0gdGhlbS4KICAgICAgICAgICAgICAgIGVyck1lc3NhZ2UgPSBleC5NZXNzYWdlOwogICAgICAgICAgICAgICAgZXJyU3RhY2tUcmFje1smKiZdfWUgPSBleC5TdGFja1RyYWNlOwogICAgICAgICAgICAgICAgZXJyU291cmNlID0gZXguU291cmNlOwogICAgICAgICAgICB9CiAgICB7WyYqJl19ICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIGxvb3AgdGhyb3VnaCBhbGwgdGhlIG5lc3RlZCBleHtbJiomXX1jZXB0aW9ucyBhbmQgY29uY2F0ZW5hdGUgdGhlbSBpbnRvIHNpbmdsZSBsb25nIHN0cmluZyB2YXJpYWJsZXMKICAgICAgICAgICAge1smKiZdfSAgICBTeXN0ZW0uRXhjZXB0aW9uIGlubmVyRXggPSBleDsKICAgICAgICAgICAgICAgIGludCBsb29wQ291bnRlciA9IDA7CiAgICB7WyYqJl19ICAgICAgICAgICAgd2hpbGUgKGlubmVyRXggIT0gbnVsbCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAvL3tbJiomXX0gbnVtYmVyIHRoZSBuZXN0ZWQgZXhjZXB0aW9ucwogICAgICAgICAgICAgICAgICAgIGxvb3BDb3VudGVyKys7CiAgICAgICAgICAge1smKiZdfSAgICAgICAgIHN0cmluZyBzZXBhcmF0b3IgPSBcIlwiOwogICAgICAgICAgICAgICAgICAgIGlmIChsb29wQ291bnRlciAmZ3Q7IDEpCntbJiomXX0gICAgICAgICAgICAgICAgICAgICAgICBzZXBhcmF0b3IgPSBcIjtcIjsKCiAgICAgICAgICAgICAgICAgICAgZXJyTWVzc2FnZSA9IGV7WyYqJl19cnJNZXNzYWdlICsgc2VwYXJhdG9yICsgXCJuICAgICBFeDpcIiArIGxvb3BDb3VudGVyICsgXCIpIFwiICsgaW5uZXJFeC5NZXNzYWdlOwp7WyYqJl19ICAgICAgICAgICAgICAgICAgICBlcnJTdGFja1RyYWNlID0gZXJyU3RhY2tUcmFjZSArIHNlcGFyYXRvciArIFwibiAgICAgRX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uc1wnXVwiOwogICAgICAgICAgICBYbWxOb2RlIG5vZGVFeGNlcHRpb25zIHtbJiomXX09IGV4Y2VwdGlvblhNTC5TZWxlY3RTaW5nbGVOb2RlKHN0ckV4WFBhdGhSb290KTsKCgogICAgICAgICAgICBub2RlVE1YTUwuQXBwe1smKiZdfWVuZENoaWxkKHhtbERvY05ld1RNWE1MLkltcG9ydE5vZGUobm9kZUV4Y2VwdGlvbnMsIHRydWUpKTsKCiAgICAgICAgICAgIHJldHV7WyYqJl19cm4geG1sRG9jTmV3VE1YTUw7CgogICAgICAgIH0KCgoKXCI7e1smKiZdfQ=={!{/code}!}


Second example (uses CreateElement)


{!{code}!}czozNjAxOlwiCgoJLy8gVGhpcyBjYW4gYmUgY2FsbGVkIGZyb20gYSBCdXNpbmVzc1J1bGUgd2hpY2ggcGFzc2VzIGEgVHlwZWRYbWx7WyYqJl19RG9jdW1lbnQKCS8vIGluc3RlYWQgb2YgYSBub3JtYWwgWG1sRG9jdW1lbnQKCXB1YmxpYyBzdGF0aWMgdm9pZCBBZGRJdGluZXJhcntbJiomXX15U3RlcChUeXBlZFhtbERvY3VtZW50IHR4ZCwgc3RyaW5nIHN0ZXBOYW1lLCBzdHJpbmcgYWN0aXZpdHlQb2xpY3kpCiAgICAgICAge1smKiZdfXsKCiAgICAgICAgICAgICAgICAgIC8vIGNhbiB3ZSBmaW5kIHRoZSBYUGF0aCAgaW5kaWNhdGUgZD8KICAgICAgICAgICAgICAgICB7WyYqJl19IHN0cmluZyBzdHJYUGF0aFJvb3QgPSBcIi8vKltsb2NhbC1uYW1lKCk9XCdJdGluZXJhcnlTdGVwc1wnXVwiOwogICAgICAgICAgICAgICB7WyYqJl19ICAgWG1sTm9kZSBub2RlID0gTG9jYXRlWFBhdGgodHhkLCBzdHJYUGF0aFJvb3QpOwogICAgICAgICAgICAgICAgICBpZiAobnVsbHtbJiomXX0gPT0gbm9kZSkKICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgdGhyb3cgbmV3IEV4Y2VwdGlvbihcIkFkZHtbJiomXX1JdGluZXJhcnlTdGVwOiBYUGF0aCBkaWQgbm90IHJldHVybiBhIHZhbGlkIG5vZGUsIFhQYXRoPVwiICsKICAgICAgICAgICAgICAgIHtbJiomXX0gICAgICAgICAgc3RyWFBhdGhSb290ICsgXCIgIFhNTD1cIiArIHR4ZC5Eb2N1bWVudC5PdXRlclhtbCk7CiAgICAgICAgICAgICAgICB7WyYqJl19ICB9OwoKCiAgICAgICAgICAgICAgICAgIC8vIGRldGVybWluZSB0aGUgcm9vdCBub2RlIGZvciB0aGUgZG9jdW1lbnQKICAgICAgIHtbJiomXX0gICAgICAgICAgIC8vIGlmIHRoZSBYUGF0aCBzZWxlY3RzIHRoZSBUWEQsIGl0IHdpbGwgaGF2ZSBubyByb290IGlmIGl0IGlzIGFue1smKiZdfSBYbWxEb2N1bWVudAogICAgICAgICAgICAgICAgICAvLyBpbiB0aGF0IGNhc2UsIHNpbXBseSB1c2UgdGhlIGRvY3VtZW50IGZyb217WyYqJl19IHRoZSBUWEQKICAgICAgICAgICAgICAgICAgWG1sRG9jdW1lbnQgcm9vdCA9IG5vZGUuT3duZXJEb2N1bWVudDsKICAgICAgICAgIHtbJiomXX0gICAgICAgIGlmIChudWxsID09IHJvb3QpCiAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgLy8gaWYge1smKiZdfXRoZSBYUGF0aCBzZWxlY3RzIHRoZSBUWEQsIGl0IG1heQogICAgICAgICAgICAgICAgICAgICAgICAvLyBzbyBmaXggYWNjb3JkaW57WyYqJl19Z2x5CiAgICAgICAgICAgICAgICAgICAgICAgIHJvb3QgPSB0eGQuRG9jdW1lbnQgYXMgWG1sRG9jdW1lbnQ7CiAgICAgICAgICAgIHtbJiomXX0gICAgICAgICAgICBpZiAobnVsbCA9PSByb290KSByZXR1cm47CiAgICAgICAgICAgICAgICAgIH0KCgogICAgICAgICAgICAgICAge1smKiZdfSAgLy8gY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ZCBub3QgcmV0dXJuIGEgdmFsaWQgbm9kZSwgWFBhdGg9XCIgKwogICAgICAgICAgICAgICAgICAgICAgICAgIHN0clhQYXRoUm9vdCB7WyYqJl19KyBcIiAgWE1MPVwiICsgdHhkLkRvY3VtZW50Lk91dGVyWG1sKTsKICAgICAgICAgICAgICAgICAgfTsKCiAgICAgICAgICAgICAgICAge1smKiZdfSBYbWxFbGVtZW50IG5ld1N1Yk5vZGU7CgogICAgICAgICAgICAgICAgICBub2RlTmFtZSA9IFwibmFtZVwiOwogICAgICAgICAgICAgIHtbJiomXX0gICAgbmV3U3ViTm9kZSA9IHJvb3QuQ3JlYXRlRWxlbWVudChub2RlTmFtZSk7CiAgICAgICAgICAgICAgICAgIG5ld1N1Yk5vZGUue1smKiZdfUlubmVyVGV4dCA9IHN0ZXBOYW1lOwogICAgICAgICAgICAgICAgICBuZXdOb2RlLkFwcGVuZENoaWxkKG5ld1N1Yk5vZGUpOwoKICB7WyYqJl19ICAgICAgICAgICAgICAgIG5vZGVOYW1lID0gXCJzdGF0dXNcIjsKICAgICAgICAgICAgICAgICAgbmV3U3ViTm9kZSA9IHJvb3QuQ3Jle1smKiZdfWF0ZUVsZW1lbnQobm9kZU5hbWUpOwogICAgICAgICAgICAgICAgICBuZXdTdWJOb2RlLklubmVyVGV4dCA9IFwiUEVORElOR1wiOwogIHtbJiomXX0gICAgICAgICAgICAgICAgbmV3Tm9kZS5BcHBlbmRDaGlsZChuZXdTdWJOb2RlKTsKCiAgICAgICAgICAgICAgICAgIG5vZGVOYW1le1smKiZdfSA9IFwiZXJyb3JUeXBlXCI7CiAgICAgICAgICAgICAgICAgIG5ld1N1Yk5vZGUgPSByb290LkNyZWF0ZUVsZW1lbnQobm9kZU5hbWUpO3tbJiomXX0KICAgICAgICAgICAgICAgICAgLy8gbGVhdmUgdmFsdWUgdG8gZW1wdHktc3RyaW5nIChubyBuZWVkIHRvIHNldCBJbm5lclRleHQpe1smKiZdfQogICAgICAgICAgICAgICAgICBu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CiAgICAgICAgfQoKXCI7e1smKiZdfQ=={!{/code}!}

Uncategorized  

Leave a Reply