2013-12-16 5 views
1

데이터베이스에서 가져온 값을 요소 이름으로 사용하여 생성 된 XML의 요소 및/또는 속성의 이름을 바꾸는 방법을 알아야합니다.XmlAttributeOverrides를 사용하여 객체 트리의 요소 이름 변경

<ArrayOfEntityTreeBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <EntityTreeBase EntityInfo="User"> 
    <Children> 
     <EntityTreeBase EntityInfo="User Medication"> 
     <Properties> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.75"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Celebrex</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-12-04T14:08:58.597"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>20000MG</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.76"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>08/01/2013</Value> 
      </EntityProperty> 
     </Properties> 
     </EntityTreeBase> 
     <EntityTreeBase EntityInfo="User Medication"> 
     <Properties> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.767"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Aspirin</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.77"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>5 mg</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.78"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>09/01/2013</Value> 
      </EntityProperty> 
     </Properties> 
     </EntityTreeBase> 
     <EntityTreeBase EntityInfo="User Medication"> 
     <Properties> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.783"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Celebrex</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.793"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>50 mg twice a day</Value> 
      </EntityProperty> 
      <EntityProperty CreatedDate="2013-11-14T16:41:12.8"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>10/01/2013</Value> 
      </EntityProperty> 
     </Properties> 
     </EntityTreeBase> 
    </Children> 
    <Properties> 
     <EntityProperty CreatedDate="2013-12-03T13:48:03.45"> 
     <FieldName>User First Name</FieldName> 
     <Value>John</Value> 
     </EntityProperty> 
     <EntityProperty CreatedDate="2013-12-03T11:36:31.423"> 
     <FieldName>User MI</FieldName> 
     <Value>Q</Value> 
     </EntityProperty> 
     <EntityProperty CreatedDate="2013-11-19T09:56:44.66"> 
     <FieldName>User Last Name</FieldName> 
     <Value>Public</Value> 
     </EntityProperty> 
     <EntityProperty CreatedDate="2013-11-14T16:41:12.803"> 
     <FieldName>User SSN</FieldName> 
     <Value>111-22-3333</Value> 
     </EntityProperty> 
    </Properties> 
    </EntityTreeBase> 
</ArrayOfEntityTreeBase> 

는 내가 달성해야 할 것은 이것이다 :

public class EntityProperty 
{ 
    [XmlIgnore] 
    public int FieldId { get; set; } 
    public string FieldName { get; set; } 
    [XmlIgnore] 
    public int FieldSortOrder { get; set; } 
    [XmlAttribute()] 
    public DateTime CreatedDate { get; set; } 
    [XmlIgnore] 
    public bool IsIterative { get; set; } 
    public string Value { get; set; } 
    public EntityTreeBase Entity { get; set; } 
    public EntityProperty() { } 
    public EntityProperty(int fieldId, string fieldName, int fieldSortOrder, DateTime createdDate, bool isIterative, string valueIn) 
    { 
     FieldId = fieldId; 
     FieldName = FieldName; 
     FieldSortOrder = fieldSortOrder; 
     CreatedDate = createdDate; 
     IsIterative = isIterative; 
     Value = valueIn; 
    } 
} 

public class EntityTreeBase 
{ 
    [XmlIgnore] 
    public long EntityId { get; set; } 
    [XmlIgnore] 
    public long? ParentEntityId { get; set; } 
    [XmlIgnore] 
    public int EntityDefinitionId { get; set; } 
    [XmlIgnore] 
    public int DestinationId { get; set; } 
    [XmlIgnore] 
    public int Level { get; set; } 
    [XmlAttribute("EntityInfo")] 
    public string EntityDefinitionName { get; set; } 
    public EntityTreeBaseCollection Children { get; set; } 
    public EntityPropertiesCollection Properties { get; set; } 
    public EntityTreeBase() { } 
    public EntityTreeBase(long entityId, long? parentEntityId, int entityDefinitionId, int destinationId, int level, string entityDefinitionName) 
    { 
     EntityId = entityId; 
     ParentEntityId = parentEntityId; 
     EntityDefinitionId = entityDefinitionId; 
     DestinationId = destinationId; 
     Level = level; 
     EntityDefinitionName = entityDefinitionName; 
    } 
    public bool HasChildren 
    { 
     get { return (Children != null && Children.Count > 0); } 
    } 
    public bool HasProperties 
    { 
     get { return (Properties != null && Properties.Count > 0); } 
    } 
    public static EntityTreeBase BuildTree(EntityTreeBaseCollection collection, EntityTreeBase parent) 
    { 
     parent.Properties = EntityPropertiesCollection.GetProperties(parent.DestinationId, parent.EntityId, parent.EntityDefinitionId); 
     parent.Children = new EntityTreeBaseCollection(); 
     foreach (EntityTreeBase item in EntityTreeBaseCollection.FindChildEntities(collection, parent.EntityId)) 
     { 
      parent.Children.Add(BuildTree(EntityTreeBaseCollection.GetChildren(item.EntityId, item.Level, item.DestinationId), item)); 
     } 
     if (!parent.HasChildren) 
     { 
      parent.Children = null; 
     } 
     if (!parent.HasProperties) 
     { 
      parent.Properties = null; 
     } 
     return parent; 
    } 
} 
: 여기
<UserInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <User> 
    <UserMedications> 
     <UserMedication> 
     <MedicationProperties> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.75"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Celebrex</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-12-04T14:08:58.597"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>20000MG</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.76"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>08/01/2013</Value> 
      </MedicationProperty> 
     </MedicationProperties> 
     </UserMedication> 
     <UserMedication> 
     <MedicationProperties> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.767"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Aspirin</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.77"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>5 mg</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.78"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>09/01/2013</Value> 
      </MedicationProperty> 
     </MedicationProperties> 
     </UserMedication> 
     <UserMedication> 
     <MedicationProperties> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.783"> 
      <FieldName>Medication Name</FieldName> 
      <Value>Celebrex</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.793"> 
      <FieldName>Medication Dosage</FieldName> 
      <Value>50 mg twice a day</Value> 
      </MedicationProperty> 
      <MedicationProperty CreatedDate="2013-11-14T16:41:12.8"> 
      <FieldName>Medication Prescribed Date</FieldName> 
      <Value>10/01/2013</Value> 
      </MedicationProperty> 
     </MedicationProperties> 
     </UserMedication> 
    </UserMedications> 
    <UserProperties> 
     <UserProperty CreatedDate="2013-12-03T13:48:03.45"> 
     <FieldName>User First Name</FieldName> 
     <Value>John</Value> 
     </UserProperty> 
     <UserProperty CreatedDate="2013-12-03T11:36:31.423"> 
     <FieldName>User MI</FieldName> 
     <Value>Q</Value> 
     </UserProperty> 
     <UserProperty CreatedDate="2013-11-19T09:56:44.66"> 
     <FieldName>User Last Name</FieldName> 
     <Value>Public</Value> 
     </UserProperty> 
     <UserProperty CreatedDate="2013-11-14T16:41:12.803"> 
     <FieldName>User SSN</FieldName> 
     <Value>111-22-3333</Value> 
     </UserProperty> 
    </UserProperties> 
    </User> 
</UserInformation> 

내 개체 예를 들어

, 여기에 내 현재 프로세스에서 잠재적 인 XML 출력입니다

잘하면 분명 하듯이 "Medication"또는 "User"라는 객체 유형이 없기 때문에 이것들을 유추해야합니다. f ROM 데이터. 그래서 데이터의 값을 사용하여 요소 이름을 변경하는 방법을 알아야하지만 각 요소 이름이 연관된 EntityDefinitionName을 기반으로 변경되도록 개체 트리를 크롤링하는 방법을 알아야합니다. 직렬화 이전에 객체 트리를 채우기 위해 재귀를 사용하고 있습니다. 나는 다음과 같은 코드 내 XmlRoot 이름을 변경하기 위해 노력 알고

XmlAttributeOverrides xmlOverrides = new XmlAttributeOverrides(); 
XmlAttributes attribs = new XmlAttributes(); 
XmlRootAttribute rootAttr = new XmlRootAttribute(); 
rootAttr.ElementName = collection.Find(e => e.Level == 1).EntityDefinitionName.Replace(" ", ""); 
attribs.XmlRoot = rootAttr; 

을하지만 그 요소 또는 노드와 관련된 EntityDefinitionName에 따라 각 요소의 이름을 변경하는 방법을 알아낼 필요가있다.

미리 감사드립니다.

답변

0

사용자 지정 직렬화를 사용하면 XSLT 스타일 시트를 사용하여 질문을 해결하는 다른 방법을 제시 할 수 있습니다.

var xml = File.Open("input.xml", FileMode.Open); // or any stream 
var xslTrans = new XslCompiledTransform(); 
xslTrans.Load(XmlReader.Create(File.Open("yourxslfile.xlst", FileMode.Open))); 
var output = File.Create("output.xml"); // or a stream 
var xw = XmlWriter.Create(output); 
xslTrans.Transform(XmlReader.Create(xml), xw); 
xw.Close(); 
: 이것은 당신이 당신의 C# 코드의 실행을 변환을 사용할 수있는 코드가

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
       exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="ArrayOfEntityTreeBase"> 
     <UserInformation > 
     <xsl:apply-templates /> 
     </UserInformation> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User']"> 
    <User> 
     <xsl:apply-templates /> 
    </User> 
    </xsl:template> 

    <xsl:template match="Children"> 
    <UserMedications> 
     <xsl:apply-templates /> 
    </UserMedications> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User Medication']"> 
    <UserMedication> 
     <xsl:apply-templates /> 
    </UserMedication> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User Medication']/Properties"> 
    <MedicationProperties> 
     <xsl:apply-templates /> 
    </MedicationProperties> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User']/Properties"> 
    <UserProperties> 
     <xsl:apply-templates/> 
    </UserProperties> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User']/Properties/EntityProperty"> 
    <xsl:element name="UserProperty"> 
     <xsl:copy-of select="@*"/> 
     <xsl:copy-of select="*" /> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="EntityTreeBase[@EntityInfo='User Medication']/Properties/EntityProperty"> 
    <xsl:element name="MedicationProperty"> 
     <xsl:copy-of select="@*"/> 
     <xsl:copy-of select="*" /> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

:

이 스타일 시트는 원하는 출력 XML에 입력 XML 변환