는이 같은 같은 XML을 가지고 말할 수 있습니다?기본 LINQ 구문
저는 LINQ/XML을 가지고 놀기 시작했습니다. 오늘은 필자가 생각한 것입니다. System.Xml.Linq.XNode 및 System.Xml.Linq.XObject
는이 같은 같은 XML을 가지고 말할 수 있습니다?기본 LINQ 구문
저는 LINQ/XML을 가지고 놀기 시작했습니다. 오늘은 필자가 생각한 것입니다. System.Xml.Linq.XNode 및 System.Xml.Linq.XObject
이 답변에 의해 정의 된 질문자가 자신의 XML을 변경 고려 완전히 잘못로
var q = from c in loaded.Descendants("field")
where (string)c.Attribute("required") == "Y" &&
// how to check the parent node (message) has an attribute (id="P")
select (string)c.Attribute("name");
foreach (string name in q)
Console.WriteLine(name);
남자 나는 c.Parent와 가까운 soo이었다. 그러나 단지 didnt는 속성을 본다. .. 고맙다! –
당신은 선조() 메서드 또는 부모 속성을 사용합니다. 이것을 삭제해야합니까?
var q = from c in loaded.Descendants("field")
where (string)c.Attribute("required") == "Y" &&
c.Parent.Attribute("id").Value == "P"
select (string)c.Attribute("name");
Xml을 추가하는 것은 적절한 해결책을 통해 혼란이 있기 때문에 사용했습니다. 여기입니다, null 또는 선택 속성의 문제로
자른 :
XDocument loaded = XDocument.Parse(@"
<message name=""Person"" id=""P"">
<field name=""FirstName"" required=""Y"" />
<field name=""LastName"" required=""Y"" />
<field name=""Sex"" required=""N"" />
<message name=""Car"" id=""C"">
<field name=""Make"" required=""Y"" />
<field name=""Model"" required=""Y"" />
<field name=""Year"" required=""N"" />
</message>
</message>");
당신은뿐만 아니라 편집
(from c in myXML.Descendants("field")
where c.Attribute("required").Value == "Y" &&
c.Parent.Attribute("id").Value == "P"
select c.Attribute("name").Value).ToList().ForEach(s => Console.WriteLine(s.ToString()));
다음을 수행하여 별도의 foreach 루프 추한 캐스트를 제거 할 수 있습니다 null 속성을 처리하기위한 확장 메소드를 추가하는 방법에 대한 예를 들어, 확장 메소드의 두 번째 매개 변수 인 경우 null 속성을 사용할 기본 값을 전달할 수 있습니다.
XDocument loaded = XDocument.Parse(@"
<messages>
<message name=""Person"" id=""P"">
<field name=""FirstName"" required=""Y"" />
<field name=""LastName"" required=""Y"" />
<field name=""Sex"" required=""N"" />
</message>
<message name=""Car"" id=""C"">
<field name=""Make"" required=""Y"" />
<field name=""Model"" required=""Y"" />
<field name=""Year"" required=""N"" />
</message>
</messages>");
대신의 모든 분야를 찾고 다음 각 부모를 확인하십시오
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
namespace MyTestApp
{
class Program
{
static void Main(string[] args)
{
XDocument myXML = XDocument.Load(@"C:\file.xml");
(from c in myXML.Descendants("field")
where c.Attribute("required")
.GetAttributeValueOrDefault("N") == "Y" &&
c.Parent.Attribute("id").Value == "P"
select
c.Attribute("name").Value).ToList().ForEach(s => Console.WriteLine(s.ToString()));
Console.ReadLine();
}
}
public static class XLinqHelper
{
// extension method that handles an xattribute and returns the provided default if the Xattrib is null
public static string GetAttributeValueOrDefault(this XAttribute s, string defaultValue)
{
string retVal;
if (s == null)
retVal = defaultValue;
else
retVal = s.Value;
return retVal;
}
}
}
나는 유효한 XML을 만들기 위해 메시지 루트 요소와 닫는 태그를 추가 당신이 조사 할 분야가 적어 질 수 있도록 당신이 끼어 들었던 한 부모를 찾으십시오 :
IEnumerable<string> fields =
loaded.Root.Elements()
.Where(m => m.Attribute("id").Value == "P")
.Single()
.Elements("field")
.Where(f => f.Attribute("required").Value == "Y")
.Select(f => f.Attribute("name").Value);
편집 :
message 요소에 다른 종류의 요소가 포함되어있는 경우 자식 요소에 대해 "field"지정자가 추가되었습니다.
편집 2 :
나는 실제 데이터의 부분 집합들과 함께 작업 예제를 넣어 :
XDocument loaded = XDocument.Parse(@"
<fix major=""4"" minor=""4"">
<header>
</header>
<trailer>
</trailer>
<messages>
<message name=""ResendRequest"" msgtype=""2"" msgcat=""admin"">
<field name=""BeginSeqNo"" required=""Y"" />
<field name=""EndSeqNo"" required=""Y"" />
</message>
<message name=""Reject"" msgtype=""3"" msgcat=""admin"">
<field name=""RefSeqNum"" required=""Y"" />
<field name=""RefTagID"" required=""N"" />
<field name=""RefMsgType"" required=""N"" />
<field name=""SessionRejectReason"" required=""N"" />
<field name=""Text"" required=""N"" />
<field name=""EncodedTextLen"" required=""N"" />
<field name=""EncodedText"" required=""N"" />
</message>
</messages>
</fix>");
IEnumerable<string> fields =
loaded.Root.Element("messages").Elements("message")
.Where(m => m.Attribute("name").Value == "Reject")
.Single()
.Elements("field")
.Where(f => f.Attribute("required").Value == "Y")
.Select(f=>f.Attribute("name").Value);
당신은 그와 같은 XML이없는, 유효한 XML 문서 아니다. 보유하고있는 실제 XML을 게시 할 수 있습니까? – Guffa