Xamarin.Form에서 Xml 파싱
Xamarin.Form에서 Portable type의 프로젝트를 생성하였을때는 XmlDocument를 사용할 수 없습니다. 대신에 XDocument를 이용하여 xml 문자열 또는 파일을 읽어서 특정 클라스로 파싱할 수 있습니다.
Xdocument를 사용하기 위하여 System.Xml.Linq를 참조해야 합니다.
다음은 Xml 문자열입니다.
<myXml>
<pi>aaaaa</pi>
<ty>4</ty>
<ct>20170610T085835</ct>
<ri>vvvvv</ri>
<lt>20170610T085835</lt>
<et>20180610T085835</et>
<st>84</st>
<mni>9007199254740991</mni>
<cs>1</cs>
<con>6</con>
</myXml>
다음은 위의 xml을 특정 클래스로 파싱하는 예입니다.
using System;
using System.IO;
using System.Xml.Linq;
namespace MyRoomControl
{
public static class XmlManager
{
public static M2mResponse ParsyXdocument(string xmlString)
{
XDocument doc = XDocument.Parse(xmlString);
XElement ele = XElement.Parse(doc.ToString());
var myClass = new MyExampleClass()
{
pi = ele.Element("pi").Value,
ty = ele.Element("ty").Value,
ct = ele.Element("ct").Value,
ri = ele.Element("ri").Value,
lt = ele.Element("lt").Value,
et = ele.Element("et").Value,
st = ele.Element("st").Value,
mni = ele.Element("mni").Value,
cs = ele.Element("cs").Value,
con = ele.Element("con").Value
};
return myClass;
}
}
}
'Programming' 카테고리의 다른 글
맥에서 Visual Source Code로 파이썬 사용하기 (0) | 2017.12.03 |
---|---|
Xamarin 자마린에서 안드로이드 설치 패치지 apk 생성하기 (0) | 2017.11.24 |
cross-compile (1) | 2017.05.29 |
맥에서 비주얼 스튜디오 삭제 (0) | 2017.05.29 |
맥에서 비쥬얼 스튜디오로 안드로이드 개발하기 (0) | 2017.03.09 |