2013-04-16 4 views
2

데이터베이스를 쿼리하고 결과를보고하는 프로그램이 있습니다. 여기 내 쿼리입니다 :Linq를 사용하여 XML을 읽는 방법

<?xml version="1.0" encoding="utf-8"?> 
<!--XML Database.--> 
<config> 
    <goals> 
      <service1>4</service1> 
      <service2>3</service2> 
     </goals> 
</config> 

는 XML (위치 C:\config.xml) 보고서 :

Select Service, Total 
From Services 
Where dtcreated between @startdate and @enddate 

라는 이름의 데이터 세트 dataset 보고서이 reportviewer1

내가 여기에 xml 파일이

은 XML 파일입니다 to Reportviewer2

내가 원하는 것은 쿼리 결과와 서비스의 가치를 이메일로 보내고 싶습니다. xml 파일의 목표

Dim xelement As XElement = xelement.Load("C:\Config.xml") 
Dim employees As IEnumerable(Of XElement) = xelement.Elements() 

    Dim test = _ 
    <html><body><table><tr><th>Service</th><th>Total Sold</th><th>Goals</th></tr> 
     <%= From service In Me.Dataset.datatable.AsEnumerable _ 
      Select <tr><td><%= service.Service_Category.ToString %></td> 
         <td><%= service.Total_Sold.ToString %></td> 
         <%= From XMLFile In xelement.Descendants("goals").AsEnumerable _ 
          Select <td><%= XMLFile.Descendants("goals").Value %></td> %></tr> %> 
     </table></body></html> 

지금은에 같은 테이블에있는 XML 파일의 값을 포함하면된다 싶습니다 무엇을 : 내 코드는 다음과 같습니다 데이터 세트에서 이메일로 전송하는 테이블을 함께 넣어 LINQ를 사용할 수 있어요 위의 데이터 빌드 내에서 세 번째 열에보고하십시오. 세 번째 열은 "목표"가되고 4, 3을 내 설정 파일별로 표시해야한다는 것을 알 수 있습니다. 위의 Linq 및 HTML 빌드에서 XML 파일의 값을 어떻게 포함시킬 수 있습니까? 당신은 내가 config 파일에서 "4"및 "3"누락 볼 수 있듯이

Service Total Sold Goals 
Service1  51 
Service2  12 

: 여기 내 출력이 현재 모습입니다. 여기

내가 내 출력과 같이 할 것입니다 :

Service Total Sold Goals 
Service1 51   4 
Service2 12   3 
+0

덧붙여 객체

 // read the xml file into memory var doc = GetXmlDataFromFileName("FormsPersistence"); var data = (from attrib in doc.Descendants("FormsPersistenceDs").Descendants("Properties") select new FormsPersistence { Id = Guid.NewGuid(), FormName = attrib.Element("FormName").ValueTrim(), ControlName = attrib.Element("ControlName").ValueTrim(), Property = attrib.Element("Property").ValueTrim(), PropertyValue = attrib.Element("PropertyValue").ValueTrim() }).ToList(); 

파일 템플릿에 XML 데이터를 읽을 수있는 코드가, 난 그냥 VB.NET에서 인라인 XML을 언급 ... _OUCH! _ 템플릿 파일 리소스의 문제점은 무엇입니까? –

+0

템플릿 파일 리소스를 사용하는 방법을 알고 있고 위의 표에 정보를 채우는 데 계속 같은 문제가있을 수 있습니다. 빌드 후 구성 파일을 편집 할 수 있습니까? 목표는 월간/연간로 변경되므로 목표를보고 할 수있는 방법이 필요합니다. 데이터베이스를 쿼리하고 목표 설정을 살펴보고 테이블을 작성한 다음 이메일로 테이블을 전송하는 자동화 된 프로그램입니다. – Shmewnix

+0

@GrantThomas 지루한 테스트 설정 및 주장에서 사용할 수 있으면 좋겠다면 C#을 작성할 때 인라인 XML이되어야합니다. –

답변

2

당신은 쿼리를 얻을 수 linqpad를 사용하여, (희망 객체가 이미 존재하거나 하나를 만들 수 있습니다)와 LINQ to XML을 사용하여이 작업을 수행 할 수 아래로.

코드는 필요하지 결과를 트리밍 때문에 .Value하기 위해 XML 문서를

  private static XDocument GetXmlDataFromFileName(string fileName) 
      { 
       // read the xml file into memory 
       return XDocument.Load(new FileStream(String.Format(@"C:\<some path>\{0}.xml", fileName), FileMode.Open)); 
      } 

확장 방법을 얻을 수 있지만, 값이 존재하지 않은 경우에 나는 그것이 끝에서 공간을 어떻게 사용 또는 빈 문자열.

public static class XmlExtensions 
{ 
    public static String ValueTrim(this XElement element) 
    { 
     return element != null ? element.Value.Trim() : ""; 
    } 
} 

<?xml version="1.0" standalone="yes"?> 
<FormsPersistenceDs> 
    <Properties> 
    <FormName>Form1</FormName> 
    <ControlName>mainNaviBar</ControlName> 
    <Property>Width</Property> 
    <PropertyValue>275</PropertyValue> 
    </Properties> 
</FormsPersistenceDs> 
관련 문제