XDocument 네임 스페이스 C #
-
21-12-2019 - |
문제
나는 MVC에 어떤 신청서를 작성하고, 나는 그 물건을 쓰는 것을 할 필요가있다. 다음과 같이 XML 응답을 가져와야합니다.
<?xml version="1.0" encoding="utf-8"?>
<getAllShepherdsResponse xmlns="http://www.sheeps.pl/webapi/1_0">
<shepherds>
<shepherd>
<errors>
<error code="1">error1</error>
<error code="-2147483647">error2</error>
<error code="2147483647">error3</error>
</errors>
<shepherdId>1</shepherdId>
<name>name1</name>
<sheeps>
<sheep>
<id>1</id>
<colour>colour1</colour>
<createdOn>1900-01-01T01:01:01+01:00</createdOn>
</sheep>
<sheep>
<id>-2147483647</id>
<colour>colour2</colour>
<createdOn>0001-01-01T00:00:00+01:00</createdOn>
</sheep>
</sheeps>
</shepherd>
</shepherds>
</getAllShepherdsResponse>
.
이 코드를 만들고 쉽게 볼 수 있습니다. 데이터베이스에서 XElements로 XDocument를 만듭니다 그리고 첫 xenement의 경우 나는 AddIn 네임 스페이스를 가지고 있지만 자세히 작업하고 싶습니다. 내 문제는 무엇입니까? :
protected XNamespace xn = "http://www.sheeps.pl/webapi/1_0";
XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement(xn+"getAllShepherdsResponse",
from emp in db.Shepherds.ToList()
select new XElement("shepherd",
new XElement("shepherdId", emp.Id),
new XElement("name", emp.Name),
new XElement("sheeps", from sp in emp.Sheep
select new
XElement("sheep", new XElement("id", sp.Id), new XElement("colour", sp.Colour),new XElement("createdOn", sp.CreatedOn))))));
.
셰퍼드 근처에서 "xmlns"를 얻는 이유는 무엇입니까? :
<getAllShepherdsResponse xmlns="http://www.sheeps.pl/webapi/1_0">
<shepherd xmlns="">
<shepherdId>1</shepherdId>
<name>A</name>
<sheeps>
<sheep>
<id>1</id>
<colour>Red</colour>
<createdOn />
</sheep>
<sheep>
<id>2</id>
<colour>Blue</colour>
<createdOn />
</sheep>
</sheeps>
</shepherd>
</getAllShepherdsResponse>
. 해결책
네임 스페이스에서 모든 요소를
와 같이 만들어야합니다.new XElement(xn+"getAllShepherdsResponse",
from emp in db.Shepherds.ToList()
select new XElement(xn+"shepherd",
new XElement(xn+"shepherdId", emp.Id),
new XElement(xn+"name", emp.Name),
new XElement(xn+"sheeps", from sp in emp.Sheep
select new
XElement(xn+"sheep", new XElement(xn+"id", sp.Id), new XElement(xn+"colour", sp.Colour),new XElement(xn+"createdOn", sp.CreatedOn))))));
. 제휴하지 않습니다 StackOverflow