2012-11-28 3 views
0

나는이 오류를 던지고 다음 코드 ...XmlAttributeOverrides

오류 ... 배열이 아닌 유형의

, 다음과 같은 속성을 사용할 수 있습니다 : XmlAttribute, XmlText, XmlElement 또는 XmlAnyElement.

코드 (이동 방법에서 마지막 줄은 예외를 던지고있다) ...

public void Go(Type typeToSerialize, object itemToSerialize) 
    { 
     Dictionary<string, bool> processedList = new Dictionary<string, bool>(); 
     XmlAttributeOverrides overrides = new XmlAttributeOverrides(); 

     AttachXmlTransforms(overrides, itemToSerialize.GetType(), processedList); 

     s = new XmlSerializer(typeToSerialize, overrides); 
    } 

    private static void AttachXmlTransforms(XmlAttributeOverrides overrides, Type root, 
     Dictionary<string, bool> processedList) 
    { 
     foreach (PropertyInfo pi in root.GetProperties()) 
     { 
      string keyName = pi.DeclaringType + "-" + pi.Name; 

      if ((pi.PropertyType == typeof(DateTime) || pi.PropertyType == typeof(DateTime?)) 
       && !processedList.ContainsKey(keyName)) 
      { 
       XmlAttributes attributes = new XmlAttributes(); 

       attributes.XmlElements.Add(new XmlElementAttribute(pi.Name)); 
       //attributes.XmlAnyAttribute = new XmlAnyAttributeAttribute(); 
       attributes.XmlAttribute = new XmlAttributeAttribute("dval"); 

       //attributes.XmlIgnore = true; 

       processedList.Add(keyName, true); 


       overrides.Add(pi.DeclaringType, pi.Name, attributes); 
      } 

      if (pi.MemberType == MemberTypes.Property && !pi.PropertyType.IsPrimitive 
       && pi.PropertyType.IsPublic && pi.PropertyType.IsClass 
       && pi.PropertyType != typeof(DateTime)) 
      { 
       AttachXmlTransforms(overrides, pi.PropertyType, processedList); 
      } 
     } 
    } 

나는이 외부 요구 사항입니다 (단 날짜 시간 요소 (dval) 속성을 추가하려고 해요)이에서

... 이것에

<CreatedDate>01/01/2012</CreatedDate> 

...

<CreatedDate dval="01/01/2012">01/01/2012</CreatedDate> 

일반 비 배열 유형 요소에 속성을 추가하는 방법이 있습니까?

+0

주 객체를 변환 : 아주 impor의입니다 이와 같이 작성된 시리얼 라이저를 재사용하지 않거나 메모리 누수가 발생합니다. –

답변

0

나는이에게 다른 방법을 approching 결국 ...

소요, 매개 변수로 XmlElementAttribute을지지 않습니다 1) ... XML

2) 다음을 실행에

 using (MemoryStream memStm = new MemoryStream()) 
     { 
      // Serialize the object using the standard DC serializer. 
      s.WriteObject(memStm, graph); 

      // Fix the memstream location. 
      memStm.Seek(0, SeekOrigin.Begin); 

      // Load the serialized document. 
      XDocument document = XDocument.Load(memStm); 

      foreach (KeyValuePair<string, ItemToAmend> kvp in _processedDateTimes) 
      { 
       // Locate the datetime objects. 
       IEnumerable<XElement> t = from el in document.Descendants(XName.Get(kvp.Value.ProperyName, kvp.Value.PropertyNamespace)) 
              select el; 

       // Add the attribute to each element. 
       foreach (XElement e in t) 
       { 
        string convertedDate = string.Empty; 

        if (!string.IsNullOrEmpty(e.Value)) 
        { 
         DateTime converted = DateTime.Parse(e.Value); 

         convertedDate = string.Format(new MyBtecDateTimeFormatter(), "{0}", converted); 
        } 

        e.Add(new XAttribute(XName.Get("dval"), convertedDate)); 
       } 
      } 

      // Write the document to the steam. 
      document.Save(writer); 
     } 
1

나는 당신이, 내가 XmlElements가 XML 요소 아니라고 추측하고있어 명명 규칙에 의해 판단, 라인

attributes.XmlElements.Add(new XmlElementAttribute(pi.Name)) 

첫째로 문제가있는 것으로 추정하지만, 그것은 수집의의 집단.

또한

, 당신의 오류 메시지 판단의 XmlElementsAdd 방법은 대신은 XmlAttribute, XmlText, XmlElement, or XmlAnyElement.

+0

내 질문에 호출 메소드를 추가했습니다. 오류는 XmlSerializer에 의해 throw됩니다. 요소에 XmlAttributeOverrides를 사용하여 특성을 추가 할 수 있습니까? – paulio