2011-03-24 5 views
0

XML 파일이 있기 때문에 XML 파일에서 값을 추출하여 다른 XML 파일에 저장해야합니다.XML 파일에 대한 객체를 생성하려고 시도했습니다.

질문 :

  1. 또 다른 사람이 결과 XML 파일의 "스키마"를 만드는 것입니다. 그 사람이 저에게 값의 삽입을 자동화 할 수있는 것을 제공 할 수 있습니까? XML에서 무엇인가를 추출해야 할 필요가 있습니까? 아니면 모든 변환을 XSLT와 같은 방식으로 수행 할 수 있습니까?

  2. 아래의 XML 구조에 문제가 있습니까? 나는 객체를 생성하기 위해 xsd2code를 사용해 보았지만 LoadFromFileMethod를 사용할 때 아무것도로드하지 않습니다 - 매우 구체적이지는 않지만 "중첩 된 부모"가 XSD.exe 및 xsd2code에 대한 문제를 일으킨다 고 말한 기사를 읽었습니다.

<Section> 
<Form id="1"...> 
<Control id="12523"..> <--Some have this some don't 
    <Property name="Color">Red</Property> 
    <Property name="Size">Large</Property> 
</Control> 
</Form> 
<Form id="2"...> 
    <Property name="Color">Blue</Property> 
    <Property name="Size">Large</Property> 
</Form> 
<Form id="3"...> 
    <Property name="Color">Red</Property> 
    <Property name="Size">Small</Property> 
</Form> 
</Section> 
어떤지도 주셔서 감사합니다!

답변

2

XSLT는 XML 변환 도구입니다.

지금까지 당신의 XML이가는대로, 많은 응용 프로그램에서이를 교체해야합니다 :

<Property name="Color">Red</Property> 

로 :

<Color>Red</Color> 

몇 가지 이유 : 당신이 원하는 경우

  1. 요소의 내용을 어떤 식 으로든 제한하는 스키마 (예 : 값 목록 중 하나)를 작성하려면 요소가 ident 그것의 이름으로 ifiable; name 속성이 "Color"인 Property 요소에 대해 하나의 스키마를 쓸 수 없으며 name 특성이 "Size"인 Property 요소에 대해 다른 스키마를 작성할 수 없습니다.

  2. 요소 이름이 의미있는 경우 XPath 조건자를 작성하는 것이 더 쉽습니다. 예를 들어, Form[Color = 'Red']은 쓰기가 훨씬 쉽습니다 (읽기). Form[Property[@name='Color' and .='Red']]

  3. 위의 내용은 거의 동일한 방식으로 XML에 대해 Linq 쿼리를 작성한 경우에도 마찬가지입니다. Element.Descendants("Color")Element.Descendents("Property").Where(x => x.Attributes["name"] == "Color")을 비교하십시오.

일반적으로 이름이 지정된 요소를 사용하는 것이 적절한 경우도 있습니다. 위의 주장은 결정적인 것이 아닙니다. 그러나 만약 당신이 그것을하려고한다면, 당신은 좋은 이유가 있어야합니다.

+0

+1 속성 탐구. –

1

XLST는 xml을 스키마에서 다른 스키마로 변환하는 가장 좋은 방법입니다. 그것은 무엇을 위해 만들어진 것입니다. http://w3schools.com/xsl/default.asp은 훌륭한 XSLT 자습서입니다. 정말로 필요한 것은 xslt 파일을 작성하기위한 XML의 몇 가지 예 또는 스키마입니다.

또한 XML이 잘 보입니다.

1

XSLT는 변환하려는 경우 솔루션 일 수 있지만 코드의 값을 사용하여 작업해야하는 경우 LINQ to XML을 사용하면 훨씬 쉽게 작업을 수행 할 수 있습니다.

1

여기에 XSLT를 사용하겠다. 시작하기에 좋은 예가있다.

복사 빈 C# 프로젝트에이 샘플 코드 :

static void Main(string[] args) { 
    const string xmlPath = "source.xml"; 
    const string xslPath = "transform.xsl"; 
    const string outPath = "out.xml"; 

    try { 
     //load the Xml doc 
     var xmlDoc = new XPathDocument(xmlPath); 

     //load the Xsl 
     var xslDoc = new XslCompiledTransform(); 
     xslDoc.Load(xslPath); 

     // create the output file 
     using (var outDoc = new XmlTextWriter(outPath, null)) { 
      //do the actual transform of Xml 
      xslDoc.Transform(xmlDoc, null, outDoc); 
     } 
    } 
    catch (Exception e) { 
     Console.WriteLine("Exception: {0}", e.ToString()); 
    } 
} 

는 source.xml 파일로 위의 예제 XML 코드를 작성하고 transform.xsl 파일에 다음 XSL 코드를 넣어 :

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output indent="yes" method="xml" /> 
<xsl:template match="/"> 
    <xsl:apply-templates /> 
</xsl:template> 

<xsl:template match="Section"> 
    <OtherSection> 
     <xsl:apply-templates /> 
    </OtherSection> 
</xsl:template> 

<xsl:template match="Form"> 
    <OtherForm> 
     <xsl:attribute name="id"> 
      <xsl:value-of select="@id" /> 
     </xsl:attribute> 
     <xsl:apply-templates /> 
    </OtherForm> 
</xsl:template> 

<xsl:template match="Control"> 
    <OtherControl> 
     <!-- converts id attribute to an id tag --> 
     <id> 
      <xsl:value-of select="@id" /> 
     </id> 
     <xsl:apply-templates /> 
    </OtherControl> 
</xsl:template> 

<xsl:template match="Property"> 
    <OtherProperty> 
     <!-- converts name attribute to an id attribute --> 
     <xsl:attribute name="id"> 
      <xsl:value-of select="@name" /> 
     </xsl:attribute> 
     <xsl:value-of select="."/> 
    </OtherProperty> 
</xsl:template> 

</xsl:stylesheet> 

결과 out.xml은 xsl이 작업을 수행하고 잘하면 시작할 수있는 방법을 알려줍니다.

XSLT에 대한 자세한 내용은 W3Schools에서 tutorial을 찾아보십시오.

관련 문제