diff --git a/src/encoding/xml/marshal.go b/src/encoding/xml/marshal.go index d0899c0fa6..63f8e2aa87 100644 --- a/src/encoding/xml/marshal.go +++ b/src/encoding/xml/marshal.go @@ -924,6 +924,9 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error { switch finfo.flags & fMode { case fCharData: + if err := s.setParents(&noField, reflect.Value{}); err != nil { + return err + } if vf.CanInterface() && vf.Type().Implements(textMarshalerType) { data, err := vf.Interface().(encoding.TextMarshaler).MarshalText() if err != nil { @@ -967,6 +970,9 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error { continue case fComment: + if err := s.setParents(&noField, reflect.Value{}); err != nil { + return err + } k := vf.Kind() if !(k == reflect.String || k == reflect.Slice && vf.Type().Elem().Kind() == reflect.Uint8) { return fmt.Errorf("xml: bad type for comment field of %s", val.Type()) diff --git a/src/encoding/xml/marshal_test.go b/src/encoding/xml/marshal_test.go index 5e9718c20c..394855782e 100644 --- a/src/encoding/xml/marshal_test.go +++ b/src/encoding/xml/marshal_test.go @@ -340,6 +340,16 @@ type OuterOuterStruct struct { OuterStruct } +type NestedAndChardata struct { + AB []string `xml:"A>B"` + Chardata string `xml:",chardata"` +} + +type NestedAndComment struct { + AB []string `xml:"A>B"` + Comment string `xml:",comment"` +} + func ifaceptr(x interface{}) interface{} { return &x } @@ -995,6 +1005,14 @@ var marshalTests = []struct { ExpectXML: ``, Value: &OuterOuterStruct{OuterStruct{IntAttr: 10}}, }, + { + ExpectXML: `test`, + Value: &NestedAndChardata{AB: make([]string, 2), Chardata: "test"}, + }, + { + ExpectXML: ``, + Value: &NestedAndComment{AB: make([]string, 2), Comment: "test"}, + }, } func TestMarshal(t *testing.T) {